0

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.

GeverAL
  • 21
  • 4
  • 1
    As far as the sockets aspect goes, it does appear that your client sends the length of the image array and then the image byte array itself, and the server receives the image, so what is the problem? If something isn't working please provide specific details. – President James K. Polk Mar 07 '21 at 22:44
  • Thank you for trying to help, the server crush every time Im sending the photo from the client. – GeverAL Mar 08 '21 at 06:02
  • Sorry for the discomfort, I've just started using android studio, so I dont know a lot about it. I edited the question, I added the 'LogCat' errors, hope it giving you the information you need :) . – GeverAL Mar 08 '21 at 10:34
  • 2
    This error suggests that your image view can only be changed by the thread who created its instance. Easy fix will be to call UI thread and set your image inside its scope. – mohammed ahmed Mar 08 '21 at 11:30

1 Answers1

1

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.

GeverAL
  • 21
  • 4