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);
}
}