Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitmap decodeStream OutOfMemory Exception

I'm using my own implementation of ViewFlow example for Android in my application. I'm downloading encrypted images from web service and than save 'em on SD Card. I'm using viewflow to decrypt images on the fly and show them. But the problem is that when user start changing the images too fast it's throwing me an OutOfMemoryException and all the information that I've found/test doesn't work for my situation. Here is what I'm using :

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.image_item, null);
    }

    try {
        File bufferFile = new File(ids.get(position));
        FileInputStream fis   = new FileInputStream(bufferFile);

        Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
        SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
        IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        CipherInputStream cis = new CipherInputStream(fis, cipher);

        BitmapFactory.Options o = new BitmapFactory.Options();
        final int REQUIRED_SIZE=300*1024;

        //Find the correct scale value. It should be the power of 2.
        int width_tmp= o.outWidth, height_tmp= o.outHeight;
        int scale=1;
        while(true){
            if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                break;
            width_tmp/=2;
            height_tmp/=2;
            scale*=2;
        }

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;

        Bitmap ops = BitmapFactory.decodeStream(cis,null,o2);
        ((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ops);
        cis.close();
        fis.close();

        System.gc();

    } catch (Exception e) {
        e.printStackTrace();
        ((ImageView) convertView.findViewById(R.id.imgView)).setImageResource(R.drawable.image_unavailablee);
    }

    return convertView;
}

And it's still throwing me that exception on line :

((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ops);

with this exception :

java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=6791KB, Allocated=3861KB, Bitmap Size=26006KB)

Any ideas how to fix that?

like image 342
Android-Droid Avatar asked Dec 24 '11 10:12

Android-Droid


2 Answers

Just for reference to anyone who is dealing with large bitmaps there is an article that show how is the best way to deal with this kind of problems to avoid OutofMemory!

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Hope it helps!

like image 176
Leonardo Arango Baena Avatar answered Nov 06 '22 23:11

Leonardo Arango Baena


The REQUIRED_SIZE should contain the max dimension (width, height in pixels) like

  final int REQUIRED_SIZE = 1024; // 1024 pixels wide or long.

You also missed couple of lines for getting image bounds into BitmapFactory.Options o before calculating the scaling factor.

    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(cis, null, o);

    //The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

Then use o.outWidth and o.outHeight for calculating the scale factor. You might need to fetch cis again for the actual decoding of the stream.

Update:

Also, You can make the following variables as members of adapter and initialize in the constructor.

SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());

This should work with no issues.

like image 4
Ronnie Avatar answered Nov 06 '22 22:11

Ronnie