如何克服“位图大小超过VM预算”的错误?

时间:2011-11-15 04:31:15

标签: android bitmap

java.lang.OutOfMemoryError: bitmap size exceeds VM budget.

我正在使用此imageloader类从互联网更新图像到gridview。有些时候我必须下载大约1000张图片,但得到相同的错误。 请帮助我。

  public class ImageLoader {


   public static  HashMap<String, Bitmap> cache=new HashMap<String, Bitmap>();

   private File cacheDir;
   int i=0;
   File f;
   BitmapDrawable b;
    public ImageLoader(Context context){
        //Make the background thead low priority. This way it will not affect the UI performance
        photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);

       GridViewConfig.clearFilelist();
       GridViewConfig.file_list=new ArrayList<String>();


        //Find the dir to save cached images
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
        else
            cacheDir=context.getCacheDir();
        if(!cacheDir.exists())
            cacheDir.mkdirs();
    }

    final int stub_id=R.drawable.imagelistback_106x133;
    public void DisplayImage(String url, Activity activity, ImageView imageView)
    {

        if(cache.containsKey(url))
        {

           /* if(Common.isgallery)
            {
            b=null;
            b=new BitmapDrawable(cache.get(url));
            Bitmap bb=Bitmap.createScaledBitmap(cache.get(url), 300, 300, true);
            imageView.setImageBitmap(bb);
            bb.recycle();
            }
            else
            {*/

                 imageView.setImageBitmap(cache.get(url));
           /* }*/
        }
        else
        {
            //Toast.makeText(activity, "ELSE in dISPLAY iMAGES", Toast.LENGTH_LONG).show();
            queuePhoto(url, activity, imageView);
            imageView.setImageResource(stub_id);
        }   



    }

    private void queuePhoto(String url, Activity activity, ImageView imageView)
    {
        //This ImageView may be used for other images before. So there may be some old tasks in the queue. We need to discard them. 
        photosQueue.Clean(imageView);
        PhotoToLoad p=new PhotoToLoad(url, imageView);
        synchronized(photosQueue.photosToLoad){
            photosQueue.photosToLoad.push(p);
            photosQueue.photosToLoad.notifyAll();
        }

        //start thread if it's not started yet
        if(photoLoaderThread.getState()==Thread.State.NEW)
            photoLoaderThread.start();
    }
    public void Createfile(String url)
    {
        String filename=String.valueOf(url.hashCode());
        f=new File(cacheDir, filename);
        GridViewConfig.file_list.add(f.getAbsolutePath());
    }
    public Bitmap getBitmap(String url) 
    {

        //I identify images by hashcode. Not a perfect solution, good for the demo.
        String filename=String.valueOf(url.hashCode());
        //String filename=GridViewConfig.sku_list.get(i);
        File f=new File(cacheDir, filename);
        GridViewConfig.file_list.add(f.getAbsolutePath());
        /*if(Common.firstLoading)
        {
        GridViewConfig.file_list.add(f.getAbsolutePath());
        i++;
        Log.v("gooooooooogle file list", "file list size "+i);

        }*/
        //from SD cache
        Bitmap b = decodeFile(f);
        if(b!=null)
            return b;

        //from web
        try {
            Bitmap bitmap=null;
            InputStream is=new URL(url).openStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Exception ex){
           ex.printStackTrace();
           return null;
        }
    }

    //decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=50;
            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++;
            }
            Log.v("", "Scale"+scale);
            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }

    //Task for the queue
    private class PhotoToLoad
    {
        public String url;
        public ImageView imageView;
        public PhotoToLoad(String u, ImageView i){
            url=u; 
            imageView=i;
        }
    }

    PhotosQueue photosQueue=new PhotosQueue();

    public void stopThread()
    {
        photoLoaderThread.interrupt();
    }

    //stores list of photos to download
    class PhotosQueue
    {
        private Stack<PhotoToLoad> photosToLoad=new Stack<PhotoToLoad>();

        //removes all instances of this ImageView
        public void Clean(ImageView image)
        {
            for(int j=0 ;j<photosToLoad.size();){
                if(photosToLoad.get(j).imageView==image)
                    photosToLoad.remove(j);
                else
                    ++j;
            }
        }
    }

    class PhotosLoader extends Thread {
        public void run() {
            try {
                while(true)
                {
                    //thread waits until there are any images to load in the queue
                    if(photosQueue.photosToLoad.size()==0)
                        synchronized(photosQueue.photosToLoad){
                            photosQueue.photosToLoad.wait();
                        }
                    if(photosQueue.photosToLoad.size()!=0)
                    {
                        PhotoToLoad photoToLoad;
                        synchronized(photosQueue.photosToLoad){
                            photoToLoad=photosQueue.photosToLoad.pop();
                        }
                        Bitmap bmp=getBitmap(photoToLoad.url);
                        cache.put(photoToLoad.url, bmp);
                        if(((String)photoToLoad.imageView.getTag()).equals(photoToLoad.url)){
                            BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad.imageView);
                            Activity a=(Activity)photoToLoad.imageView.getContext();
                            a.runOnUiThread(bd);
                        }
                    }
                    if(Thread.interrupted())
                        break;
                }
            } catch (InterruptedException e) {
                //allow thread to exit
            }
        }
    }

    PhotosLoader photoLoaderThread=new PhotosLoader();

    //Used to display bitmap in the UI thread
    class BitmapDisplayer implements Runnable
    {
        Bitmap bitmap;
        ImageView imageView;
        public BitmapDisplayer(Bitmap b, ImageView i)
        {
            bitmap=b;
            imageView=i;
        }
        public void run()
        {
            if(bitmap!=null)
                /*if(Common.isgallery)
                {
                    b=null;
                    b=new BitmapDrawable(bitmap);
                    Bitmap bit=Bitmap.createScaledBitmap(bitmap, 300, 300, true);
                    imageView.setImageBitmap(bit);
                    bit.recycle();
                }
                else
                {*/
                imageView.setImageBitmap(bitmap);

                /*}*/
            else
                imageView.setImageResource(stub_id);
        }
    }

    public void clearCache() {
        //clear memory cache
        cache.clear();

        //clear SD cache
        File[] files=cacheDir.listFiles();
        for(File f:files)
            f.delete();
    }

}

4 个答案:

答案 0 :(得分:1)

我建议:

  • SoftReference<Bitmap>中使用WeakReference<Bitmap>cache代替简单的位图。这样你就不会面对OOM;
  • 使用options for decoding(正如Awais Tariq所建议的那样) - inSampleSize选项可以很好地改进内存(似乎已在您的代码中完成);
  • 在sd卡上维护缓存以下载图像。有关其他选项,请参阅this question(似乎已在您的代码中完成)。

答案 1 :(得分:0)

使用位图选项指定位图。 http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html

进一步尝试手动调用垃圾收集器(System.gc();),因为除非应用程序关闭,否则不会释放位图内存..

答案 2 :(得分:0)

您正在使用太多位图对象,但在完成工作后不会释放它们。 因此,在完成特定的位图对象后,请调用 bitmapObj.recycle();

它将释放该对象占用的内存。

答案 3 :(得分:0)

我面对同样的问题,我有永久的解决方案:

首先,你使用的任何图像都需要缩放它们以便它会消耗更少的内存并且你的“OutOfMemoryError”将会解决。

例如: 访问此链接

http://android-solution-sample.blogspot.com/search/label/Bitmap%20out%20of%20memory

解码后,您应该使用图像,以便消耗更少的内存。