1

Possible Duplicate:
Android: Strange out of memory issue while loading an image to a Bitmap object
OutOfMemoryError: bitmap size exceeds VM budget :- Android

I have an application where users can take pictures or use one already existing in the gallery, and attach to a news, after saving the news in the database (I am only saving the image path content :/ / ...), I display all news in a ListView with an image title and date. to display the images in the adapter I'm using the following:

...

image.setImageURI(null);
System.gc();            
image.setImageURI(Uri.parse(noticia.getIMAGEM()));

...

however even using the System.gc (); each new image that is loaded'm getting

12-12 14:59:37.239: E / AndroidRuntime (4997): java.lang.OutOfMemoryError: bitmap size exceeds performer VM budget

also already tried using

image.setImageURI(null);
System.gc();  

in onResume(), onPause(), onDestroy(), but nothing worked.

also read this post: [link][1]

if(!((BitmapDrawable)image.getDrawable()).getBitmap().isRecycled()){
                ((BitmapDrawable)image.getDrawable()).getBitmap().recycle();
            }
            Bitmap thumbnail = null;
            try {
                thumbnail = MediaStore.Images.Media.getBitmap(context.getContentResolver(), Uri.parse(img));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             image.setImageBitmap(thumbnail); 

but then I get the following error:

12-12 15:28:42.879: E/AndroidRuntime(5296): java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@40756520

and besides when my list view to scroll up and down it keeps crashing, I believe that it is requesting the images ..

I do not know what else to do, any tips?

EDIT:

I found another post in the same solution to this problem worked perfectly, however I believe it would need a cache for the images because when scrolling the list up or down the one she fought, and this is very bad for the User Experience . Any idea to solve?

the following code:

in adapter: ...

String imagePath = getPath(Uri.parse(img));
image.setImageBitmap(decodeSampledBitmapFromResource(imagePath, 85, 85));

...

methods:

public String getPath(Uri uri)  
    { 
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    } 

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 2;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }
    return inSampleSize;

    }

    public static Bitmap decodeSampledBitmapFromResource(String resId,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(resId, options);
    }
1
  • I'm not taking a picture from the web or remote server, the image is saved to the appliance itself and I retrieve the path that previously saved in the database Commented Dec 12, 2012 at 17:35

1 Answer 1

0

It sounds like you want to ge thumbnail from local gallery. There is a better solution directly provided by the SDK :

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap thumbnail = Thumbnails.getThumbnail(context.getContentResolver(), /*ID of the local item*/, Thumbnails.MINI_KIND, options);
        // Calculate inSampleSize
        options.inSampleSize = UIUtils.calculateInSampleSize(options, reqWidth, reqHeight);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        thumbnail = Thumbnails.getThumbnail(context.getContentResolver(), /*ID of the local item*/, Thumbnails.MINI_KIND, options);
Sign up to request clarification or add additional context in comments.

3 Comments

this line options.inSampleSize UIUtils.calculateInSampleSize = (options, 85, 85); UIUtils is referenced, but not to any references to import, perhaps some code of your own design?
I found another post in the same solution to this problem worked perfectly, I'll edit the post and put the code, however I believe they need a cache for the images because when scrolling the list up or down the one she fought, and this is very bad experience for the User. Any idea to solve?
Sure, this article explains all that you need here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.