1

I am trying to receive image in client socket of android from server socket of C# and C# server works fine for a C# client and the received image is exactly the same as the image sent. The problem is when i try to receive image in client socket of Android, it shows only a white line on the top of the image. I think android app is not reading whole Byte array of data even tough i am sending and receiving the size of image before the image is sent or received.

Here is my code:

class imageReceiver implements Runnable {

    Bitmap bitmap;
    Bitmap btmp;
    private InputStreamReader isr;
    private String mesg ;
    BufferedReader br ;
    MainActivity mn;
    DataInputStream dis;
    byte[] data;

    public imageReceiver(MainActivity mn){
        this.mn = mn;
    }
    @Override
    public void run() {
        if (s.isConnected()) {
            try {    
                final InputStream in = s.getInputStream();
                dis = new DataInputStream(in);
                byte[] readMsgLen = new byte[4];
                dis .read(readMsgLen,0,4);
                final int length = readMsgLen[0]<<24 
                       | (readMsgLen[1] & 0xFF) << 16 
                       | (readMsgLen[2] & 0xFF) <<8 
                       | (readMsgLen[3] & 0xFF);

                data = new byte[length];
                dis.readFully(data,0,data.length);
                bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

                mn.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(mn.getApplicationContext(), 
                                       "Image received of length: "+length,
                                       Toast.LENGTH_SHORT).show();

                        if (bitmap==null){
                            Toast.makeText(mn.getApplicationContext(), 
                                           "Image is not received",
                                           Toast.LENGTH_SHORT).show();

                        }
                        else {
                            Toast.makeText(mn.getApplicationContext(), 
                                           "Image received",
                                           Toast.LENGTH_SHORT).show();
                            Picasso.get()
                                   .load(getImageUri(mn,bitmap))
                                   .fit()
                                   .into(mn.iv);

                        }               
                    }
                });
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public Uri getImageUri(Context inContext , Bitmap inImage){
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.PNG,100,bytes);
        String path = MediaStore.Images.Media.insertImage(
                inContext.getContentResolver(),
                inImage,"Sssss",null);
        return Uri.parse(path);
    }
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
Bilal Hassan
  • 37
  • 1
  • 6
  • Have you checked that the value that you are computing for `length` is correct? – Stephen C Jul 30 '19 at 13:16
  • length is always shown '512' in android,may be I am trying to send & receive Screenshot that's why length is same always.I have no idea how to check length in 'Visual Studio', i am unable to do it in 'MessageBox' as it shows 'System.Byte[]'. – Bilal Hassan Jul 30 '19 at 15:19
  • Here is image sending code: byte[] sendBytes = new byte[12500]; sendBytes = BmpToBytes(capture()); byte[] datasize = new byte[4]; datasize = BitConverter.GetBytes((Int32)sendBytes.Length); sStream.Write(datasize,0,4); sStream.Write(sendBytes, 0, sendBytes.Length); sStream.Flush(); – Bilal Hassan Jul 30 '19 at 15:24

0 Answers0