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)
Related
I have a photo (jpeg) format in an Android device. Instead of posting the file as a file using HTTP I prefer to convert that to a string and post it as string using http to a spreadsheet. I understand jpeg files are encoded and opening them as string shows funny characters. My question is if I send these characters as string using http, can I get them back on the other side using a binary file editor and save them as jpeg?
One thing you can do is, on client side:
1.Convert image to byte array.
Path path = yourImageFile.toPath();
byte[] byteArray = Files.readAllBytes(path);
2.Encode the byteArray to Base64 String.
String encodedString = Base64.encodeBase64URLSafeString(byteArray);
3.That's it, send via http.
I'm not really sure what you mean by a binary file editor, but from server side you can retrieve the image like this (if using Java):
byte[] decodedByte = decodedBase64.decodeBase64(encodedString);
FileOutputStream out = new FileOutputStream("newPathToYourImage");
out.write(decodedByte);
out.close();
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'm currently using the following method to convert a bitmap into a Base64 encoded string, however for very big images I'm getting an OutOfMemory error:
public static String convertBitmapToBase64String(Bitmap bmp) throws OutOfMemoryError
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArrayImage = baos.toByteArray();
String base64EncodedImg = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
return base64EncodedImg;
}
I saw this answer, however it uses IOUtils: https://stackoverflow.com/a/24877734/720175
Is it possible to do convert a large bitmap into a base64 encoded string using only standard libraries normally included?
bmp.compress(Bitmap.CompressFormat.JPEG, (AS LOWER), baos);
bmp.compress(Bitmap.CompressFormat.JPEG, 50, baos);
You can reduce the quality level then its working fine
It is most likely because your final string is too big as well as the temporary space it needs to do the conversion. If you want very large string like this you will need to use a stream in and stream out process.
That is, convert the strings from an input stream and stream the results back out to a file.
You dont have to load the large file at once, load it in chunks which will not lead to outofmemoryexception.
Refer this link to convert large file in chunks of data to base64 string
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...
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.