0

I'm trying to reduce the file size from gallery before upload to the server with base64. I've tried ALL the suggestions from stackoverflow & elsewhere I've found on the internet without success.

For images > 2MB, they seem to be reduced in size once written on the server (down to ~500KB). However, for images < 500KB they seem to be bigger than original file size once decoded and written on the server (again ~500KB). It seems like there's threshold that base64 can't get lower beyond). Is it true? Any other way I can reduce the image file size and upload to server programmatically?

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
adbert
  • 11
  • 4
  • You can use _GZip_ – Piyush Sep 06 '16 at 07:36
  • I don't want to use GZip since I want to permanently reduce the file size (on the fly) and send to server. Ideally under 100KB regardless of what the original size was because I'll retrieve them again to populate the listview. – adbert Sep 06 '16 at 08:44
  • Check [this](http://stackoverflow.com/questions/37299490/android-reduce-file-size-for-camera-captured-image-to-be-less-than-500-kb) – Piyush Sep 06 '16 at 09:28
  • I've seen that. Let me follow it again. From memory, I think I couldn't get it to compile. – adbert Sep 06 '16 at 10:48

1 Answers1

-1
private String string(Bitmap bitmap){
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
    return Base64.encodeToString(stream.toByteArray(), Base64.DEFAULT);
}

Pass bitmap into string method, to convert into String. compressFormat is in PNG which automatically lower the size and quality.

  • This is what I've been trying. The difference where is I've tried with Bitmap.CompressFormat.JPG instead of PNG. I believe it's reduced the file size. However, once encoded to by base64 and sent to server, written down to disk it's expanded to around ~500KB (some cases turned out to be larger than the original). It seems decoding base64 from the server end is really expanding the JPG. – adbert Sep 06 '16 at 08:48
  • Same thing happened with me. –  Sep 06 '16 at 09:05