Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying an image from an InputStream

I am able to send strings from my Android mobile phone to my computer, and vice versa. However, I want to send an image from my computer and display it to the mobile phone. In my case, the computer is the server and the mobile phone is the client.

This is part of my code on the server side:

socket = serverSocket.accept();
dataOutputStream = new DataOutputStream(socket.getOutputStream());
captureScreen("C:\\Users\\HP\\Desktop\\capture.png");

File f = new File("C:\\Users\\HP\\Desktop\\capture.png");
byte [] buffer = new byte[(int)f.length()];
dataOutputStream.write(buffer,0,buffer.length);
dataOutputStream.flush(); 

Note that captureScreen() is a method that successfully takes a screenshot of the server and save it as a .PNG image in the above path.

Now, on the client side which is the Android mobile phone, if I have an ImageView control, how to read the image sent from the computer as an InputStream and display it on the ImageView?

Furthermore, did I write successfully the image to the dataOutputStream? I would be glad if any one helps me !

like image 389
WassiM ZgheiB Avatar asked Nov 21 '25 12:11

WassiM ZgheiB


1 Answers

You can call the setImageBitmap(Bitmap bm) of your ImageView.

http://developer.android.com/reference/android/widget/ImageView.html

How you get the image data to your client: it depends on the solution you have chosen, but technically you can use the same libraries that you would use for pure Java.

You can use android.graphics.BitmapFactory to create the Bitmap from your stream.

http://developer.android.com/reference/android/graphics/BitmapFactory.html

Bitmap bitmap1 = BitmapFactory.decodeStream(inputStream);
Bitmap bitmap2 = BitmapFactory.decodeFile(filename);
like image 53
gaborsch Avatar answered Nov 23 '25 02:11

gaborsch