Need to display an image recieved through socket in android - android

Hey I need to display an image read from socket in my android application.The android application is basically a client it receives image from a java server.The java server writes a png file using ImageIO.write() function.I am new to android .Can somebody please help me

read the image first into byte[] array and then construct bitmap from bytes using BitmapFactory. It could look something like this:
byte[] buffer = new byte[imgSize];
//read all data to the buffer
BitmapFactory.decodeByteArray(buffer, 0, buffer.length);

Related

Cons and pros of using Base64 encode and decode image in Android

This is block of code I used for convert from Image into Base64:
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.image);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String imgStr =Base64.encodeToString(ba,Base64.DEFAULT);
But I know we also can convert image into Binary data.
So just want to know the cons and pros of using Base64 encode image in Android. Should I use Base64 or Binary data to transfer image through message.
You should not send images through gcm server. It was not designed for that at all. You should be sending links, and other information, which is then used to get the data from a server after being received on the client (server-> client pushes).
Base64 is often used when transferring binary data as a string, which is stupid in this case because it increases the size of the data needing transfered, and you could easily do a POST to a server with the binary data directly.

convert Bitmap to Byte array without compress method in android

I want to upload an image to my website via my android app so for that i want to convert my image to Byte[].
i have used the following code but not work for me..
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG,0, bos);
byte[] data=bos.toByteArray();
So please share with me any other way to convert an Image to Byte[]..
Use ByteBuffer:
array = new byte[w*h*4];
Buffer dst = ByteBuffer.wrap(array);
bmp.copyPixelsToBuffer(dst);
and use array the way you want...

Send byte array of a Bitmap using http post in android?

In my application I need to call an API Using Http Post to send some information to the web server. So I need to append userid, name, phoneNo and image to the url. I need to convert the image to a byte array and append it to url. I used the following code but it didn't work for me. I just got a byte array like this [B#4055dd90
My code is
Bitmap bitmap1 = ((BitmapDrawable) d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
Can Some one please help Me ?
Thanks in advance !
Two things:
Images can get pretty big. So, you may be better off using a JPG, as you can compress it (a PNG does not compress). So, you'd use Bitmap.CompressFormat.JPEG and set the amount of compression you want.
When sending in your post, you should encode the byte[] like this: Base64.encodeBase64String( bitmapdata);
Let me know if that works for you. OH, and don't forget to unencode it on your server side.
You can not send the images in the byte array form directly to server. You have to encode it into Base64 String with the help of Base64 Class. Encode Bitmap byte array to Base64 string and send that string to server with the help of HTTPPost method. If you have any doubt then you can comment.

The image is too small when I try to upload it using Facebook Graph API and Android

Simply put I need to capture image using camera and upload it to facebook via my android application. And I successfully did that. The problem is when the photo posted in facebook, it's just too small and in low resolution while the image I took is in high resolution.
I understand that: in order to upload to facebook, i need to convert the captured image which is in bitmap format into byte array. So i have method for that:
public static byte[] convertBitmapToByteArray(Bitmap bm){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.PNG, 100, bos);
byte[] bitmapdata = bos.toByteArray();
return bitmapdata;
}
Then to upload the image to facebook, i have code below where byteData is byte array I converted from bitmap image using the method above.
parameters.putString("message", "Test");
parameters.putByteArray("source", byteData);
String facebookResponse = facebookInstance.request(albumId+"/photos",parameters,"POST");
return facebookResponse;
I am pretty sure the problem is my convertBitmapToByteArray method since the method is to compress the bitmap image and turn it into byte array, and this made my image into low resolution image. However I can't seem to find the way to upload the image without converting it into byte array first. Any solution for me?
Alright even this thread is old, i found out the answer. It's not the problem of CompressFormant.JPEG or CompressFormat.JPG. Simply put, intent in android isn't designed to carry big data like image from activity through activity. I need to save the image from intent to sd card first before able to pull it out from there. It's my solution.

Android: get image from base64binary format

I use web service to get image. The service response contains image in base64Binary format. I try to decode response data with Base64.decode() (http://iharder.sourceforge.net/current/java/base64/). See my code below:
byte[] data = Base64.decode(responseString);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bmp);
decodeByteArray always return null.
I try to save data in .png file. I can open this file on my PC and in the Android File Manager application. But preview activity of File Manager couldn't open this file.
Then i try to parse this data using .NET client with Convert.Base64() method. And this image have been processing successfully. Then i compare byte array in image created with android client and .NET client. The differences were in sign of bytes. .NET uses unsigned bytes but Java use only signed bytes. Is this is a reason of my problem?
Is anybody have the same problem in decoding of base64Binary?
Here is one solution, and for me is working (knowing that the format in which the image comes from the server through the web service is base64binary)
decodedIcon[] = null;
byte[] bb = (resposeString).getBytes("utf-8");
decodedIcon = Base64.decodeBase64(bb);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedIcon, 0,
decodedIcon.length);
//then you get the image view and setImageBitmap(bitmap)
PS:
Base64.decodeBase64 comes from the library org.apache.commons.codec.binary.Base64;
You should have commons-codec-1.3.jar included in the assets folder
the version doesn't have to be 1.3
Thanks to one of my friends for this hint.

Categories

Resources