OutOfMemoryError:位图大小超出VM预算

| 抱歉,这似乎是一个重复的问题,但是我认为我不符合已经发布的任何建议。 我的应用程序中最多包含20张图片。在玩了一会儿后,我得到了OutOfMemoryError。 奇怪的是,我没有任何静态引用,而且我已经搜索了可能的内存泄漏,可以保证到目前为止我还没有找到。 无论如何,20张图像(平均PNG为100KB)不是那么多。而且我已经实现了视图缓存,位图的SoftReference持有人,等等。 平均20KB 100KB的PNG图片足以杀死我的应用程序吗?认真吗?我该如何摆脱呢?我也关注了这篇很棒的帖子 http://blog.jteam.nl/2009/09/17/exploring-the-world-of-android-part-2/ 还有其他想法吗? 这是ImageCache:
public class AsyncImageLoader {

    private final String TAG = getClass().getSimpleName();
    private Context mContext;
    private HashMap<String, SoftReference<Bitmap>> mImageCache;

    public AsyncImageLoader(Context context) {
        mContext = context;
            mImageCache = new HashMap<String, SoftReference<Bitmap>>();
    }

    public Bitmap loadImage(final String identifier, final String imagePath, final ImageCallback imageCallback) {

        if (mImageCache.containsKey(imagePath)) {
            SoftReference<Bitmap> softReference = mImageCache.get(imagePath);
            Bitmap bitmap = softReference.get();
            if (bitmap != null) {
                Log.i(TAG, \"Retrieving image from cache: \" + imagePath);
                return bitmap;
            }
        }

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message message) {
                imageCallback.imageLoaded((Bitmap) message.obj, imagePath, identifier);
            }
        };

        new Thread() {

            @Override
            public void run() {
                Bitmap bitmap = loadImageFromPath(imagePath);
                mImageCache.put(imagePath, new SoftReference<Bitmap>(bitmap));
                Message message = handler.obtainMessage(0, bitmap);
                handler.sendMessage(message);
            }

        }.start();

        return null;
    }

    public Bitmap loadImageFromPath(String path) {

        if(!GeneralUtilities.isEmpty(path)) {
            Log.i(TAG, \"Loading image: \" + path);
            InputStream imageInputStream = null;

            try {               
                final AssetManager assetManager = mContext.getResources().getAssets(); 
                imageInputStream = assetManager.open(path);

                Bitmap bitmap = GeneralUtilities.decodeFile(imageInputStream);

                imageInputStream.close();

                return bitmap;
            } catch (final IOException e) {
                Log.e(TAG, e.getMessage());
            }        
        }

        return null;
    }

    public interface ImageCallback {
        public void imageLoaded(Bitmap imageBitmap, String imagePath, String identifier);
    }
}
方法GeneralUtilities.decodeFile是:
public static Bitmap decodeFile(InputStream is){
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(is, null, o);

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

        //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;
        return BitmapFactory.decodeStream(is, null, o2);  
      }
在ArrayAdapter的getView中,我有类似以下内容:
final ImageView itemImage = cache.getHistoryImage();        
        //final ImageView itemFrame = cache.getFrame();

        String filename = item.getFilename().trim();

        itemImage.setTag(\"front_\" + filename);

        Bitmap cachedImage = mAsyncImageLoader.loadImage(\"front_\" + filename, filename, new ImageCallback() {

            public void imageLoaded(Bitmap imageBitmap, String imagePath, String identifier) {

                ImageView imageViewByTag = (ImageView) mGallery.findViewWithTag(identifier);
                if (imageViewByTag != null) {
                    imageViewByTag.setImageBitmap(imageBitmap);
                }
            }
        });

        itemImage.setImageBitmap(cachedImage);
    
已邀请:
Android框架中似乎存在一个错误,尽管Google似乎否认了它。 您是否已阅读8488期? http://code.google.com/p/android/issues/detail?id=8488 我不确定这是否适用于您的代码-但您可以在设置/更新ImageView上的图像之前尝试这些建议。 基本上,它归结为调用Bitmap.recycle(),使引用为空(在您的情况下可能是不友好的)和显式调用System.gc()。 垃圾回收器似乎异步运行,即使可以释放内存,新垃圾回收器也可能失败。     

要回复问题请先登录注册