Android - Reducing Image File Size

I have a URI image file and I want to reduce its size in order to upload it. The size of the original image file depends on the mobile phone (maybe 2 MB, or maybe 500 KB), but I want the final size to be about 200 KB so that I can download it.
From what I read, I have (at least) 2 options:

  • Using BitmapFactory.Options.inSampleSize to select the source image and get a smaller image;
  • Using Bitmap.compress to compress an image that determines the quality of compression.

What is the best choice?




I thought at first changing the size / height of the image until the width or height exceeds 1000 pixels (something like 1024x768 or others), and then compress the image with a decrease in quality until the file size is above 200 KB. Here is an example:

int MAX_IMAGE_SIZE = 200 * 1024; // max final file size Bitmap bmpPic = BitmapFactory.decodeFile(fileUri.getPath()); if ((bmpPic.getWidth() >= 1024) && (bmpPic.getHeight() >= 1024)) { BitmapFactory.Options bmpOptions = new BitmapFactory.Options(); bmpOptions.inSampleSize = 1; while ((bmpPic.getWidth() >= 1024) && (bmpPic.getHeight() >= 1024)) { bmpOptions.inSampleSize++; bmpPic = BitmapFactory.decodeFile(fileUri.getPath(), bmpOptions); } Log.d(TAG, "Resize: " + bmpOptions.inSampleSize); } int compressQuality = 104; // quality decreasing by 5 every loop. (start from 99) int streamLength = MAX_IMAGE_SIZE; while (streamLength >= MAX_IMAGE_SIZE) { ByteArrayOutputStream bmpStream = new ByteArrayOutputStream(); compressQuality -= 5; Log.d(TAG, "Quality: " + compressQuality); bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpStream); byte[] bmpPicByteArray = bmpStream.toByteArray(); streamLength = bmpPicByteArray.length; Log.d(TAG, "Size: " + streamLength); } try { FileOutputStream bmpFile = new FileOutputStream(finalPath); bmpPic.compress(Bitmap.CompressFormat.JPEG, compressQuality, bmpFile); bmpFile.flush(); bmpFile.close(); } catch (Exception e) { Log.e(TAG, "Error on saving file"); } 

Is there a better way to do this? Should I try to use all 2 methods or only one? Thanks

+55
android bitmap image-uploading image-resizing
Jun 16 '12 at 6:11
source share
3 answers

Using Bitmap.compress() , you simply specify the compression algorithm and, by the way, the compression operation takes a fairly large amount of time. If you need to play with sizes to reduce the memory allocation for your image, you need to use image scaling with Bitmap.Options , first calculating the borders of the bitmap images and then decoding it to the specified size.

The best sample I've found on StackOverflow is this one .

+29
Jun 16 '12 at 6:18
source share

Most of the answers I found were just the parts that I had to put together to get the working code , which is posted below

  public void compressBitmap(File file, int sampleSize, int quality) { try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = sampleSize; FileInputStream inputStream = new FileInputStream(file); Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, options); inputStream.close(); FileOutputStream outputStream = new FileOutputStream("location to save"); selectedBitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); outputStream.close(); long lengthInKb = photo.length() / 1024; //in kb if (lengthInKb > SIZE_LIMIT) { compressBitmap(file, (sampleSize*2), (quality/4)); } selectedBitmap.recycle(); } catch (Exception e) { e.printStackTrace(); } } 

2 sampleSize parameters and quality play an important role

sampleSize is used to subexpress the original image and return a smaller image , i.e. SampleSize == 4 returns an image that is 1/4 of the width / height of the original.

used to indicate the compressor , the input range is from 0 to 100. 0 means compress for small size, 100 means compress for maximum quality

+1
Nov 04 '16 at 9:42 on
source share

BitmapFactory.Options - reduces the size of the image (in memory)

Bitmap.compress () - reduces image size (on disk)




Refer to this link for more information on using both of them: https://android.jlelse.eu/loading-large-bitmaps-efficiently-in-android-66826cd4ad53

0
Aug 20 '19 at 19:12
source share



All Articles