Skip to content Skip to sidebar Skip to footer

How To Solve The Out Of Memory Issue While Displaying Image In Android?

I am working on a quiz application. In that I am displaying the question and options in a list. I have kept a next button at the top and when the next button is clicked, I am calli

Solution 1:

The OutOfMemory Error occurs when some of the images may be too large to display.

To fix this you do something like this:

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);

Solution 2:

Scale down the images to the ImageView's dimensions based on the screen size before displaying them so that you don't display a high res image on a low res screen. It will save a lot of memory.

Solution 3:

This may help you to reduce size of an Image pragmatically

public Bitmap decodeFile(File f) {
    try {
        // Decode image size
        BitmapFactory.Optionso=newBitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(newFileInputStream(f), null, o);
        // The new size we want to scale tofinalintREQUIRED_SIZE=70;

        // Find the correct scale value. It should be the power of 2.intscale=1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE
                && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;
        // Decode with inSampleSize
        BitmapFactory.Optionso2=newBitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(newFileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    returnnull;
}

Post a Comment for "How To Solve The Out Of Memory Issue While Displaying Image In Android?"