10

I have an Android application that is very image intensive. I'm currently using Bitmap.createScaledBitmap() to scale the image to a desired size. However, this method requires that I already have the original bitmap in memory, which can be quite sizable.

How can I scale a bitmap that I'm downloading without first writing the entire thing out to local memory or file system?

emmby
  • 99,783
  • 65
  • 191
  • 249
  • Possible duplicate of [Resize a large bitmap file to scaled output file on Android](https://stackoverflow.com/q/3331527/608639) – jww Sep 02 '18 at 15:45

2 Answers2

23

This method will read the header information from the image to determine its size, then read the image and scale it to the desired size in place without allocating memory for the full original sized image.

It also uses BitmapFactory.Options.inPurgeable, which seems to be a sparsely documented but desirable option to prevent OoM exceptions when using lots of bitmaps. UPDATE: no longer uses inPurgeable, see this note from Romain

It works by using a BufferedInputStream to read the header information for the image before reading the entire image in via the InputStream.

/**
 * Read the image from the stream and create a bitmap scaled to the desired
 * size.  Resulting bitmap will be at least as large as the 
 * desired minimum specified dimensions and will keep the image proportions 
 * correct during scaling.
 */
protected Bitmap createScaledBitmapFromStream( InputStream s, int minimumDesiredBitmapWith, int minimumDesiredBitmapHeight ) {

    final BufferedInputStream is = new BufferedInputStream(s, 32*1024);
    try {
        final Options decodeBitmapOptions = new Options();
        // For further memory savings, you may want to consider using this option
        // decodeBitmapOptions.inPreferredConfig = Config.RGB_565; // Uses 2-bytes instead of default 4 per pixel

        if( minimumDesiredBitmapWidth >0 && minimumDesiredBitmapHeight >0 ) {
            final Options decodeBoundsOptions = new Options();
            decodeBoundsOptions.inJustDecodeBounds = true;
            is.mark(32*1024); // 32k is probably overkill, but 8k is insufficient for some jpgs
            BitmapFactory.decodeStream(is,null,decodeBoundsOptions);
            is.reset();

            final int originalWidth = decodeBoundsOptions.outWidth;
            final int originalHeight = decodeBoundsOptions.outHeight;

            // inSampleSize prefers multiples of 2, but we prefer to prioritize memory savings
            decodeBitmapOptions.inSampleSize= Math.max(1,Math.min(originalWidth / minimumDesiredBitmapWidth, originalHeight / minimumDesiredBitmapHeight));

        }

        return BitmapFactory.decodeStream(is,null,decodeBitmapOptions);

    } catch( IOException e ) {
        throw new RuntimeException(e); // this shouldn't happen
    } finally {
        try {
            is.close();
        } catch( IOException ignored ) {}
    }

}
Community
  • 1
  • 1
emmby
  • 99,783
  • 65
  • 191
  • 249
  • 1
    You may also need to make an explicit GC call before calling BitmapFactory.decodeStream, as per this answer: http://stackoverflow.com/questions/6718855/using-caching-to-improve-scrolling-performance-in-an-android-listview-with-images/7245318#7245318 – emmby Aug 30 '11 at 15:06
  • it would probably make sense to use new / close instead of mark / reset on a FileInputStream. But the idea is good. – kellogs Sep 29 '11 at 23:55
  • 1
    Why do I get `java.io.IOException: Mark has been invalidated` on `is.reset();` – sancho21 Sep 20 '14 at 07:19
  • 1
    I also received the Mark has been invalidated error, had to change the mark to 64k for this to work for some images: is.mark(**64** * 1024) – Assaf S. Feb 04 '15 at 11:52
1

Here is my version, based on @emmby solution (thanks man!) I've included a second phase where you take the reduced bitmap and scale it again to match exactly your desired dimensions. My version takes a file path rather than a stream.

protected Bitmap createScaledBitmap(String filePath, int desiredBitmapWith, int desiredBitmapHeight) throws IOException, FileNotFoundException {
    BufferedInputStream imageFileStream = new BufferedInputStream(new FileInputStream(filePath));
    try {
        // Phase 1: Get a reduced size image. In this part we will do a rough scale down
        int sampleSize = 1;
        if (desiredBitmapWith > 0 && desiredBitmapHeight > 0) {
            final BitmapFactory.Options decodeBoundsOptions = new BitmapFactory.Options();
            decodeBoundsOptions.inJustDecodeBounds = true;
            imageFileStream.mark(64 * 1024);
            BitmapFactory.decodeStream(imageFileStream, null, decodeBoundsOptions);
            imageFileStream.reset();
            final int originalWidth = decodeBoundsOptions.outWidth;
            final int originalHeight = decodeBoundsOptions.outHeight;
            // inSampleSize prefers multiples of 2, but we prefer to prioritize memory savings
            sampleSize = Math.max(1, Math.max(originalWidth / desiredBitmapWith, originalHeight / desiredBitmapHeight));
        }
        BitmapFactory.Options decodeBitmapOptions = new BitmapFactory.Options();
        decodeBitmapOptions.inSampleSize = sampleSize;
        decodeBitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565; // Uses 2-bytes instead of default 4 per pixel

        // Get the roughly scaled-down image
        Bitmap bmp = BitmapFactory.decodeStream(imageFileStream, null, decodeBitmapOptions);

        // Phase 2: Get an exact-size image - no dimension will exceed the desired value
        float ratio = Math.min((float)desiredBitmapWith/ (float)bmp.getWidth(), (float)desiredBitmapHeight/ (float)bmp.getHeight());
        int w =(int) ((float)bmp.getWidth() * ratio);
        int h =(int) ((float)bmp.getHeight() * ratio);
        return Bitmap.createScaledBitmap(bmp, w,h, true);

    } catch (IOException e) {
        throw e;
    } finally {
        try {
            imageFileStream.close();
        } catch (IOException ignored) {
        }
    }
}
Assaf S.
  • 4,676
  • 2
  • 22
  • 18