OutOfMemoryError using bitmap setImageUri

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);
    }

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);
链接地址: http://www.djcxy.com/p/93098.html

上一篇: 位图加载outofmemory android

下一篇: OutOfMemoryError使用位图setImageUri