Optimal use of BitmapFactory.Options.inSampleSize for speed
Asked Answered
L

4

24

Thanks to Schermvlieger for asking this question on anddev.org,

I'm just copying his question to SO as nobody replied on the other site and I'm also facing the same problem.

I was wondering what would be the optimal use of BitmapFactory.Options.inSampleSize with regards to speed of displaying the image.
The documentation mentions using values that are a power of 2, so I am working with 2, 4, 8, 16 etc.

The things I am wondering about are:

  1. Should I resample down to the smallest size that is still larger than the screen resolution, or should I sample down to the size just enough to avoid an OutOfMemoryError?
  2. How would one calculate the maximum size of an image that could still be displayed without running out of memory? Does the color-depth of the image play a role as well, and the depth of the display?
  3. Is it efficient to display images via two mechanisms (BitmapFactory for large files, setImageURI() for smaller ones) I am using an ImageSwitcher by the way.
  4. Would it help creating the Bitmap, BitmapFactory.Options and inTempStorage in the beginning of the application or creating them only on the fly, when needed?
Lacerated answered 28/1, 2011 at 5:39 Comment(0)
M
12

You should always try to load and pre-scale images so that they are as close as possible to their final displayed size. Scaling images at drawing time is extremely expensive and should be avoided at all cost.

Considering the memory cost of an image, yes, the color-deptch plays a very important role. Images in ALPHA_8 format use 1 byte per pixel, images in RGB_565 or ARGB_4444 use 2 bytes per pixel and images in ARGB_8888 use 4 bytes per pixel. The depth of the display does not matter at all. You should always try to use ARGB_8888 to get the best possible quality, but 565 can be good enough if your image is opaque.

Misdoubt answered 18/2, 2011 at 9:22 Comment(3)
When you say that "scaling images at drawing time is extremely expensive" does this include options.inSampleSize also?Trafalgar
inSampleSize is used to scale images at load time, not at draw time. It is a very good way to prescale images.Misdoubt
We are using ARGB_8888 , which is producing a very hazy image on Samsung Galaxy Note 2, but it worked well on other device, bring it on ARGB_4444 is crashing the app on Samsung Galaxy Note 2 and Pad 3110, due to outOfMemoryError. Can you please help us sort out this thing.Axilla
H
4

You've asked good questions , but it all depends on your needs and how much memory you use. I recommend checking out this link for many tips regarding bitmaps: http://developer.android.com/training/displaying-bitmaps/index.html .

In short , you should consider caching , downsampling , and using a good-enough bitmap format whenever you can.

Here's my answers to your questions:

  1. Why not both? if you think there might be OOM , try to recycle old,unused bitmaps and then check again .

  2. you can calculate the (estimated) size of the bitmap :

    width*height*bytesPerPixel

    where bytesPerPixel is usually 4 or 2 (depending on the bitmap format) .

  3. Never used setImageURI , so I can't help you with that. I suggest downloading images in a background thread (using asyncTask is one way to do so) and showing them when it's ready.

  4. If there are only a few that you know that won't take a lot of the memory , i guess it's ok. I still think caching could be better.

Hoopla answered 9/2, 2013 at 12:13 Comment(0)
D
3

Here you can call the user defined method shrinkmehtod that actually send the string file path and the height and width to be reduce image to method.

 Bitmap bit=shrinkmethod(arrpath1[position], 100, 100);


            //iv.setImageURI(Uri.parse(arrpath1[position]));
            iv.setImageBitmap(bit);

This is user defined method to reduce the size of image programmatically.

Bitmap shrinkmethod(String file,int width,int height){
        BitmapFactory.Options bitopt=new BitmapFactory.Options();
        bitopt.inJustDecodeBounds=true;
        Bitmap bit=BitmapFactory.decodeFile(file, bitopt);

        int h=(int) Math.ceil(bitopt.outHeight/(float)height);
        int w=(int) Math.ceil(bitopt.outWidth/(float)width);

        if(h>1 || w>1){
            if(h>w){
                bitopt.inSampleSize=h;

            }else{
                bitopt.inSampleSize=w;
            }
        }
        bitopt.inJustDecodeBounds=false;
        bit=BitmapFactory.decodeFile(file, bitopt);



        return bit;

    }

I hope this will help you to reduce size.

Desideratum answered 6/3, 2012 at 6:46 Comment(1)
Orginally from here madhusudhanrc.blogspot.fr/2012/09/reduce-bitmap-size-using.htmlPsychedelic
P
0

Hi try out calculating the inSampleSize using this logic

private fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
    val (height: Int, width: Int) = options.run { outHeight to outWidth }
    var inSampleSize = 1

    if (height > reqHeight || width > reqWidth) {
        val halfHeight: Int = height / 2
        val halfWidth: Int = width / 2

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
            inSampleSize *= 2
        }
    }

    return inSampleSize
}
Pharaoh answered 31/3, 2020 at 10:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.