Issue
My teacher gave me an assignment, a mini project in android studio.
I want to get a good grade so Im trying to create an app that allows to transfer photos,
between 2 phones connected to the same WIFI network.(with sockets)
I have created an App that let you send a string to the server, But I cant figure out how to send a photo. (The photo choose section is done)
There is My Code of the client:
void client() throws IOException{
ipAddr = ipAddrEditText.getText().toString();
InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap imageBitMap = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
imageBitMap.compress(Bitmap.CompressFormat.PNG,0, bos);
byte[] array = bos.toByteArray();
Socket s = new Socket(ipAddr,7800);
OutputStream out = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(out);
dos.writeInt(array.length);
dos.write(array,0,array.length);
dos.flush();
dos.close();
out.close();
s.close();
}
There is the Server's code:
void server() {
ServerSocket ss = null;
try {
ss = new ServerSocket(7800);
DataInputStream dis;
int len;
byte[] data;
Socket s;
while(true) {
s = ss.accept();
InputStream is = s.getInputStream();
dis = new DataInputStream(is);
len = dis.readInt();
data = new byte[len];
dis.readFully(data,0,data.length);
photoBitmap = BitmapFactory.decodeByteArray(data,0,data.length);
img.setImageBitmap(photoBitmap);
dis.close();
s.close();
}
} catch (IOException e) {
e.printStackTrace();
}
The Server is crashing every time Im trying to transfer the photo.
------------------------------------------------
the log cat erors:
Process: com.example.theminiproject, PID: 15123
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:10554)
at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:2113)
at android.view.View.requestLayout(View.java:27069)
at android.view.View.requestLayout(View.java:27069)
at android.view.View.requestLayout(View.java:27069)
at android.view.View.requestLayout(View.java:27069)
at android.view.View.requestLayout(View.java:27069)
at android.view.View.requestLayout(View.java:27069)
at androidx.constraintlayout.widget.ConstraintLayout.requestLayout(ConstraintLayout.java:3239)
at android.view.View.requestLayout(View.java:27069)
at android.widget.ImageView.setImageDrawable(ImageView.java:600)
at androidx.appcompat.widget.AppCompatImageView.setImageDrawable(AppCompatImageView.java:104)
at android.widget.ImageView.setImageBitmap(ImageView.java:764)
at androidx.appcompat.widget.AppCompatImageView.setImageBitmap(AppCompatImageView.java:112)
at com.example.theminiproject.MainActivity2.server(MainActivity2.java:70)
at com.example.theminiproject.-$$Lambda$jVrGEiH1Ep-S9RdZnfrW_UVFTWs.run(Unknown Source:2)
at java.lang.Thread.run(Thread.java:923)
2021-03-08 12:24:31.431 15123-15123/? I/ViewRootImpl@867b75a[MainActivity]: stopped(false) old=true
2021-03-08 12:24:31.434 15123-15123/? I/ViewRootImpl@867b75a[MainActivity]: stopped(false) old=false
2021-03-08 12:24:31.441 15123-26157/? I/Process: Sending signal. PID: 15123 SIG: 9
Because Im a beginner i can not understand any of it.
Thank you.
Solution
I found the error in my code thanks to the helpers in the comments section.
a Thread can not change an ImageView, you must create a separate function to change it.
in my case Ive created a Button that change the image.
public void changePhoto(View v) {
img.setImageBitmap(photoBitmap);
}
- if you call the function trough the Thread the photo will change but the app will crush 0.1 second later.
Thanks for everyone who helped.
Answered By - GeverAL Answer Checked By - Mildred Charles (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.