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.
Related
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.
I want to send an base64 encoded picture from ruby on rails to android.
Sending the data to rails included the picture encoding and decoding works, the data transfer from rails back to android works also, but I can't decode the picture, which was encoded by rails. I always get a NullpointerException at the following marked line:
imageOutFile = new FileOutputStream(file);
byte[] bytes = Base64.decode(imageString, Base64.URL_SAFE);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
**bitmap.compress(Bitmap.CompressFormat.JPEG, 100, imageOutFile);**
The encoded string looks like the following (it's the shortest one):
_9j_4AAQSkZJRgABAQEASABIAAD_4Se-RXhpZgAASUkqAAgAAAALAA4BAgAUAAAAkgAAAA8BAgAUAAAApgAAABABAgAJAAAAugAAABIBAwABAAAAAQAAAA==
Another string is this:
_9j_4AAQSkZJRgABAQEAAQABAAD_2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH_2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH_wAARCAHgAoADASIAAhEBAxEB_8QAHAABAQEBAQEBAQEAAAAAAAAAAAoICQcGAgED_8QARRAAAQIBCAYIBAQEBwACAwAAAAYHBQEECAkZOIe3AxQVFqfWAhhWV1iWl9cXaNXnERIjJRM2VfAkJic1UZSkUmchMjf_xAAXAQEBAQEAAAAAAAAAAAAAAAAACQEH_8QAMxEBAAAEAwUGBQMFAAAAAAAAAAEGB1YCGJYDcbHV1ggXQUam8AURFjFRYaG1IYGRxcb_2gAMAwEAAhEDEQA_AIPwAAAAAAACo6rguPM3iPm-vCXEqOq4LjzN4j5vrwDbQAAAAAAABLnWM33Hqw4yoQ5UYS51jN9x6sOMqEOBiMAAAAAAAAqOq4LjzN4j5vrwlxKjquC48zeI-b68A20AAAAAAAAS51jN9x6sOMqEOVGEudYzfcerDjKhDgYjAAAAAAAAKjquC48zeI-b68JcSo6rguPM3iPm-vANtAAAAAAAAEudYzfcerDjKhDlRhLnWM33Hqw4yoQ4GIwAAAAAAACo6rguPM3iPm-vCXEqOq4LjzN4j5vrwDbQAAAAAAABLnWM33Hqw4yoQ5UYS51jN9x6sOMqEOBiMAAAAAAAAqOq4LjzN4j5vrwlxKjquC48zeI-b68A20AAAAAAAAS51jN9x6sOMqEOVGEudYzfcerDjKhDgbYsbvmb4PfdIWN3zN8HvukdtABxLsbvmb4PfdIWN3zN8HvukdtABxLsbvmb4PfdIWN3zN8HvukdtABxLsbvmb4PfdId
In Logcat I get a message like this, after the failed decoding:
--- decoder->decode returned false
I encode the string with the following method and send the string with a json object to android. The picture was uploaded with the uploader of CarrierWave.
Base64.urlsafe_encode64(File.read("public#{picture_path.to_s}"))
I have to read the file binary ("rb") and then encode it.
Base64.encode64(File.open("public#{picture_path.to_s}", "rb") {|io| io.read})
I want to send Image from Android to Server. I decoded image into Base64 String and send it to the server. I use following code to convert Image to String
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] byteArray = bao.toByteArray();
String imageToString=Base64.encodeToString(byteArray,Base64.DEFAULT);
return imageToString;
Now i am unable to convert it back to Image on server side. I tried this
byte[] imageBytes=Base64.decode(imageString);
InputStream in = new ByteArrayInputStream(imageBytes);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "jpg", new File("D:\\myImage.jpg"));
i am getting Bogus Huffman table definition exception and sometime im = null exception. plz tell me what mistake i am making
Edit: Error Message javax.imageio.IIOException: Bogus Huffman table definition at this line
BufferedImage bImageFromConvert = ImageIO.read(in);
Try this
byte[] imageBytes=Base64.decode(imageString,Base64.NO_WRAP);
InputStream in = new ByteArrayInputStream(imageBytes);
Bitmap b = BitmapFactory.decodeStream(in);
Well there might be multiple issues here. The first one I think is the fact that you convert the image bytes to String (encoding them with whatever default encoding you Android environment has) and the decoding that String back to bytes without ensuring that you use the same text encoding (and thus get the same bytes).
Why not send the bytes directly? Or better yet just upload the file directly via HTTP multi-part form. There's a tutorial on this here:
http://flo.dauran.com/194-android-uploader-une-image-sur-une-serveur-web/
(it's in french, but there's detailed code examples)
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...
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.