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
Related
I have an Bitmap image hat i convert to base64, but I want that the image to occupy less because I want to upload that image to a database and it needs to be less than a mb.
Convert from Bitmap to base 64:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
imagen.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
final String imagenPlanta = Base64.encodeToString(byteArray, Base64.DEFAULT);
Any help would be appreciated.
To make it smaller you have to get rid of information. Do something like JPEG, it will focus on removing information that is less relevant, many times a JPEG image looks just fine but is like 10 times smaller.
In general vector drawables are compiled, and accessed with their ids in execution time.
My project consist of pulling out images from a server. For now, i'm doing it for png files, but i would try it with vectors ".xml".
I'm able to download those, but i can't parse them in drawable type to show them in views.
How can i do this with a bunch of bytes representing the vectors resources and parsing them to drawables ?
It would be a huge thing for my app since it will be much much lighter, and good for those pixelized pictures.
The inflate method may be helpful, but you could use a third party library:androidsvg. Don't reproduce the wheel if possible.
If you can access the image from the server via url, use Glide to load it to your View. By so doing, you don't have to download the image to your storage before using it in an ImageView. However, if you still want to get the image as a byte array and display it in a view, use the code snippet below...
Bitmap bm = BitmapFactory.decodeFile("/path/to/yourimage.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray(); //This is the image byte
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT); //Encoded string
Furthermore, you can convert the string to a Bitmap and then pass it to your ImageView.
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap bmp= BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(bmp);
Let me know if you have any question concerning this, I'd be glad to help.
I need to give the user possibility to share his high score. I took an image (PNG) and wrote the high score on it on it. Can I convert BitMap to PNG and share it without saving the file?
You can use the boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) of the Bitmap class.
ByteArrayOutputStram baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] png = baos.toByteArray();
You will have the byte[] contained the image in png format. You can then use any API you want to share that image.
I need to store in Database images chosen from gallery. My firt idea was convert Bitmap to String and store String in Database, but now I am reading other post: saving image clicked from camera or loaded from gallery in database and retrieving it and there is suggested using byte array.
Could someone explain me diffrence, which idea is better? Maybe something else?
I just start, but I would like to write it possibly correctly.
The standard way to store an image as a byte[] in a BLOB field. Another possibility - with some overhead - is to store a Base64-encoded string.
You can use the Base64 Android class:
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
You'll have to convert your image into a byte array though. Here's an example:
Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
If you're using an older SDK library (because you want it to work on phones with older versions of the OS) you won't have the Base64 class packaged in (since it just came out in API level 8 aka version 2.2).
Check this article out for a work-around:
http://androidcodemonkey.blogspot.com/2010/03/how-to-base64-encode-decode-android.html
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)