5

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?

Android-Droid
  • 14,365
  • 41
  • 114
  • 185

6 Answers6

7

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!

Leonardo Arango Baena
  • 870
  • 1
  • 10
  • 15
  • This is the best, cleanest, and most importantly FASTEST method I have seen. Thank you so much for sharing. Sometimes the best information is what the Android group has created or blogged, but it's the last place I look. – RayHaque Apr 22 '13 at 18:59
4

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.

Ron
  • 24,175
  • 8
  • 56
  • 97
2

Taken from here: http://www.memofy.com/memofy/show/1008ab7f2836ab7f01071c2dbfe138/outofmemory-exception-when-decoding-with-bitmapfactory

Try this:

BitmapFactory.Options o = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];

Bitmap bitmapImage = BitmapFactory.decodeFile(path, options);

Instead of:

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

So, before using BitmapFactory.decodeFile() create a byte array of 16kb and pass it to temp storage in decoding process.

Hope that helps! referenced: Strange out of memory issue while loading an image to a Bitmap object

Community
  • 1
  • 1
TryTryAgain
  • 7,632
  • 11
  • 46
  • 82
2

I had the same problem over and over again.

heres my code maby its alitle overkill but I had to this cause of diffrent camera size, res etc , but you`ll have to adjust it to your needs.

            BitmapFactory.Options imageOptions = new BitmapFactory.Options();
        imageOptions.inJustDecodeBounds = true;
        ByteArrayInputStream imageByteArrayInputStream = new ByteArrayInputStream(data);
        BitmapFactory.decodeStream(imageByteArrayInputStream, null, imageOptions);
        System.gc();

        // Decode frame size
        BitmapFactory.Options frameOptions = new BitmapFactory.Options();
        frameOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), selectedFrameResourceID, frameOptions);
        System.gc();

        // Scale factor for pre scaling
        int preScaleFactor = 1;
        if (imageOptions.outWidth > frameOptions.outWidth || imageOptions.outHeight > frameOptions.outHeight) {
            preScaleFactor = Math.max(imageOptions.outWidth / frameOptions.outWidth, imageOptions.outHeight / frameOptions.outHeight);
        }

        // Decode with inSampleSize
        BitmapFactory.Options scaleOptions = new BitmapFactory.Options();
        scaleOptions.inSampleSize = preScaleFactor;

        imageByteArrayInputStream = new ByteArrayInputStream(data);
        Bitmap preScaledBitmap = BitmapFactory.decodeStream(imageByteArrayInputStream, null, scaleOptions);
        System.gc();

        Bitmap finalBitmap;

        // Scale factor for precise scaling
        // If the scaled image is not exactly the same size as the frame than resize it precisely
        if (preScaledBitmap.getWidth() != frameOptions.outWidth || preScaledBitmap.getHeight() != frameOptions.outHeight) {
            float scaleFactor = Math.max((float)((float)preScaledBitmap.getWidth() / (float)frameOptions.outWidth), (float)((float)preScaledBitmap.getHeight() / (float)frameOptions.outHeight));
            float scalePercentage = Math.min((float)((float)frameOptions.outWidth / (float)preScaledBitmap.getWidth()), (float)((float)frameOptions.outHeight / (float)preScaledBitmap.getHeight()));

            Matrix matrix = new Matrix();
            matrix.preScale(scalePercentage, scalePercentage);

            // If the capture width for the source is bigger than the actual width of the source, then set is to the max of the actual source width
            int sourceCaptureWidth = (int)(frameOptions.outWidth * scaleFactor);
            if (sourceCaptureWidth > preScaledBitmap.getWidth()) {
                sourceCaptureWidth = preScaledBitmap.getWidth();
            }

            // Same as above but than for the height
            int sourceCaptureHeight = (int)(frameOptions.outHeight * scaleFactor);
            if (sourceCaptureHeight > preScaledBitmap.getHeight()) {
                sourceCaptureHeight = preScaledBitmap.getHeight();
            }

            finalBitmap = Bitmap.createBitmap(preScaledBitmap, 0, 0, sourceCaptureWidth, sourceCaptureHeight, matrix, true);

            preScaledBitmap.recycle();
            preScaledBitmap = null;

Hope this helps

Maikel Bollemeijer
  • 6,545
  • 5
  • 25
  • 48
  • Actually I implement lazy loading in my getView function and it's working without any errors for now. I guess the whole problem was in that I'm decoding stream in getView function where it's allocating too much memory. Thanks anyway for the answer! If it's still giving me an error I'll try your solution. – Android-Droid Jan 08 '12 at 21:19
  • No problem , I'm glad you figured it out. – Maikel Bollemeijer Jan 08 '12 at 22:07
  • I would just like to tip my hat to you sir. I had invented my own routine a few years back to do this same thing, but I like yours more. Very impressive. Although I ended up stealing the Android supplied 'hints' from Leonardo Arango Baena's answer which almost seems like cheating! – RayHaque Apr 22 '13 at 18:57
1

use recycle(). It will free the native object associated with this bitmap, and clear the reference to the pixel data.

 Bitmap ops = BitmapFactory.decodeStream(cis,null,o2);
        ((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ops);
        cis.close();
        fis.close();
        ops.recycle();
        System.gc();
Sujit
  • 10,512
  • 9
  • 40
  • 45
  • While I try to use recycle it's throwing me this exception : java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@40627090, 640x960 – Android-Droid Dec 24 '11 at 11:24
  • you can use recycle() in finally block.Try to do this. – Sujit Dec 24 '11 at 11:55
0

What version of Android are you running this on? If you run it on Honeycomb or higher, you should be able to use the Eclipse memory analyzer to see where your memory is getting used.

That being said, you need to call recycle() on your bitmaps as soon as they are no longer needed or displayed (which is the problem with Sujits answer). In other words, if a Bitmap goes off the screen it would be best to recycle() it and then re-load it again when it comes back into view. Otherwise, that Bitmap is usi

To do this, call getDrawable() on your ImageView, call setImageDrawable(null) on your ImageView, and then cast the drawable to a BitmapDrawable and recycle the bitmap inside of it.

For more information about how bitmap memory worked pre Android 3.0, you can see a post I did on this issue: http://code.google.com/p/android/issues/detail?id=8488#c80

Justin Breitfeller
  • 13,737
  • 4
  • 39
  • 47