0

when i am converting image file into base64 size of image increased 3 times, can anyone please explain ?? Ideally it should increase 30% to 40%.

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] image = stream1.toByteArray();
str = Base64.encodeToString(image, Base64.NO_WRAP);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rakesh Meena
  • 59
  • 2
  • 8

2 Answers2

0

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);

and str = Base64.encodeToString(image, Base64.NO_WRAP);

are the line where you have to make changes to... 100 signifies here the compression ratio which you can alter according to your requirement. It may vary from 0 - 100 in general. And Base64.NO_WRAP can be changed accordingly if you want to wrap your data to any of the encoding formats provided as default. Just write Base64. and press ctrl + Space to see which option works for you better.

Apurv Mahesh
  • 129
  • 4
0

You are not encoding the original jpg file. If you did that the base64 encoded string would have 30 % bytes more than the original file.

Instead you load the jpg in a Bitmap and then let the Bitmap compress it to a different jpg file in memory. This second one is already much bigger then the original one. You can check that easily.

So do not use an intermediate Bitmap but encode the bytes of the original file directly.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • how can we convert file to direct base64 without intermediate bitmap ?? – Rakesh Meena Apr 18 '17 at 06:51
  • Read the bytes of the file in a loop and put them in a byte array. Then encode the array like you now do with array image. But did you do the checks i proposed? Please come with exact figures. – greenapps Apr 18 '17 at 07:50