Ofek
Ofek

Reputation: 53

Bitmap to Base64 String

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

Upvotes: 2

Views: 5289

Answers (4)

KDeogharkar
KDeogharkar

Reputation: 10959

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.

Upvotes: 1

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

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

Upvotes: 1

Neeraj Sharma
Neeraj Sharma

Reputation: 191

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;
}

Upvotes: 3

Suhas Bachewar
Suhas Bachewar

Reputation: 1230

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;
   }

Upvotes: 0

Related Questions