开发者

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

开发者 https://www.devze.com 2023-04-12 11:34 出处:网络
In my application I need to call an API Using Http Post to s开发者_开发百科end some information to the web server. So I need to append userid, name, phoneNo and image to the url. I need to convert the

In my application I need to call an API Using Http Post to s开发者_开发百科end 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:

  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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消