Chrishan
Chrishan

Reputation: 4106

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 !

Upvotes: 2

Views: 7570

Answers (2)

Steve Bergamini
Steve Bergamini

Reputation: 14600

Two things:

  1. 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.
  2. 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.

Upvotes: 5

Amit Thaper
Amit Thaper

Reputation: 2137

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.

Upvotes: 2

Related Questions