Android Bitmap for Base64 String

How to convert a large bitmap (photo taken using the phoneโ€™s camera) to a Base64 string?

+96
android base64 bitmap android-bitmap
Feb 10 '12 at 7:08
source share
6 answers

use the following method to convert a bitmap to an array of bytes:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream .toByteArray(); 

to encode base64 from a byte array use the following method

 String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT); 
+238
Feb 10 '12 at 7:20
source share

I have a quick solution. Just create an ImageUtil.java file

 import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Base64; import java.io.ByteArrayOutputStream; public class ImageUtil { public static Bitmap convert(String base64Str) throws IllegalArgumentException { byte[] decodedBytes = Base64.decode( base64Str.substring(base64Str.indexOf(",") + 1), Base64.DEFAULT ); return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length); } public static String convert(Bitmap bitmap) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); return Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT); } } 

Using:

 Bitmap bitmap = ImageUtil.convert(base64String); 

or

 String base64String = ImageUtil.convert(bitmap); 
+16
Dec 07 '16 at 22:25
source share

The problem with jeet's answer is that you load all the bytes of the image into an array of bytes, which is likely to lead to the application crash on younger devices. Instead, I will first write the image to a file and read it using the Apache Base64InputStream class. You can then create a Base64 string directly from the InputStream of this file. It will look like this:

 //Don't forget the manifest permission to write files final FileOutputStream fos = new FileOutputStream(yourFileHere); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.close(); final InputStream is = new Base64InputStream( new FileInputStream(yourFileHere) ); //Now that we have the InputStream, we can read it and put it into the String final StringWriter writer = new StringWriter(); IOUtils.copy(is , writer, encoding); final String yourBase64String = writer.toString(); 

As you can see, the above solution works directly with streams instead, avoiding the need to load all bytes into a variable, so decreasing the memory size is much lower and less likely to lead to a failure in junior class devices. There is still a problem that setting the Base64 string itself to the String variable is not a good idea, because, again, this can cause OutOfMemory errors. But at least we cut memory consumption by half by eliminating an array of bytes.

If you want to skip the step of writing to a file, you need to convert OutputStream to InputStream, which is not so easy to do (you should use PipedInputStream , but this is a bit more complicated, since two streams should always be in different streams).

+11
Jul 22 '14 at 1:57
source share

Try this, first scale your image to the required width and height, just pass the original bitmap, the required width and the required height for the following method and get a scaled bitmap as a result:

For example: Bitmap scaledBitmap = getScaledBitmap (originalBitmap, 250, 350);

 private Bitmap getScaledBitmap(Bitmap b, int reqWidth, int reqHeight) { int bWidth = b.getWidth(); int bHeight = b.getHeight(); int nWidth = bWidth; int nHeight = bHeight; if(nWidth > reqWidth) { int ratio = bWidth / reqWidth; if(ratio > 0) { nWidth = reqWidth; nHeight = bHeight / ratio; } } if(nHeight > reqHeight) { int ratio = bHeight / reqHeight; if(ratio > 0) { nHeight = reqHeight; nWidth = bWidth / ratio; } } return Bitmap.createScaledBitmap(b, nWidth, nHeight, true); } 

Now just pass the scaled bitmap to the following method and return the base64 string:

For example: String base64String = getBase64String (scaledBitmap);

 private String getBase64String(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] imageBytes = baos.toByteArray(); String base64String = Base64.encodeToString(imageBytes, Base64.NO_WRAP); return base64String; } 

To decode a base64 string back to a bitmap:

 byte[] decodedByteArray = Base64.decode(base64String, Base64.NO_WRAP); Bitmap decodedBitmap = BitmapFactory.decodeByteArray(decodedByteArray, 0, decodedString.length); 
+4
Jun 08 '17 at 10:44 on
source share

Use getPixels to get the bitmap as an array of bytes. Then you can use Base64.encodeToString for encoding. This will only work for API level 8 and above. See this question for older devices.

+1
Feb 10 '12 at 7:18
source share

All of these answers are inefficient, as they unnecessarily decode into a bitmap and then recompress the bitmap. When you take a photo on Android, it is saved in JPEG format in a temporary file that you specify when you follow the Android documents .

What you need to do is directly convert this file to a Base64 string. Here's how to do it with a simple copy-paste (in Kotlin).

 fun convertImageFileToBase64(imageFile: File): String { return FileInputStream(imageFile).use { inputStream -> ByteArrayOutputStream().use { outputStream -> Base64OutputStream(outputStream, Base64.DEFAULT).use { base64FilterStream -> inputStream.copyTo(base64FilterStream) base64FilterStream.flush() outputStream.toString() } } } } 

As a bonus, the quality of your image should be slightly improved by bypassing re-compression.

0
Feb 05 '19 at 12:07 on
source share



All Articles