2

I was trying the following code to encode a bitmap into a base64 string, but I always get wrong encoding.

By the way, filePath is an image file path.

public String encode(String filePath) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byte_arr = stream.toByteArray();
    return Base64.encodeToString(byte_arr, Base64.DEFAULT);
}

How do I know if I get a wrong encoding? I use this website:

http://codebeautify.org/base64-to-image-converter

3
  • 1
    check that you giving right path for image otherwise code is looking fine. Commented Aug 29, 2016 at 6:16
  • the path to the image is right i checked it. i dont know what is wrong Commented Aug 29, 2016 at 6:38
  • You could tray Base64.NO_PADDING instead of / orred to DEFAULT, that is without trailing =s. Commented Aug 29, 2016 at 7:02

4 Answers 4

3

Try this code it does proper encoding for me. Hope it may be help you.

public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
Sign up to request clarification or add additional context in comments.

Comments

1

as @kdeogharkar pointing out, your code is looking fine.

First, check if you have a right file path. Then check if you need a permission when accessing external file.

For handling OOM while we loading a big scale image, we can use:

File sd = Environment.getExternalStorageDirectory();
File image = new File(sd+filePath, imageName);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);

Then use the following decoding and encoding method:

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
    image.compress(compressFormat, quality, byteArrayOS);
    return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}

public static Bitmap decodeBase64(String input)
{
    byte[] decodedBytes = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
}

Example usage:

String myBase64Image = encodeToBase64(myBitmap, Bitmap.CompressFormat.JPEG, 100);
Bitmap myBitmapAgain = decodeBase64(myBase64Image);

NOTE
this answer is mixed from:

  1. https://stackoverflow.com/a/28351881/4758255
  2. https://stackoverflow.com/a/9768973/4758255

Comments

1

try to use NO_WRAP instead of DEFAULT

String stringEncode = Base64.encodeToString(byte_arr, Base64.NO_WRAP);
    return stringEncode;

And use this online tool to convert base64 string (stringEncode) to image . If it is able to generate image your base64 string is correct.

1 Comment

what is option configuration while reading file path . Is it ARGB_8888 ? @Ofek
0

Try this:

public String getImageEncodedData(String path) {
       File f = new File(path);
       Bitmap imageBitmap = null;
       try {
           imageBitmap = BitmapFactory.decodeStream(new FileInputStream(f));
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       }
       //added for testing base 64
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       imageBitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
       byte[] imageBytes = baos.toByteArray();
       String mImageEncodedString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
//       Log.e("TAG", "imageEncodedString: " + mImageEncodedString);
       return mImageEncodedString;
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.