Out of memory cache error when accessing inside the app

后端 未结 2 627
野趣味
野趣味 2020-12-22 08:37

I\'ve searched a lot but I did not understand where to is my error. First in my app I am getting images from the web if there is no net I am getting them from created databa

相关标签:
2条回答
  • 2020-12-22 09:08

    memory management is a pretty extensive and advanced topic but I'll post a very important tip all in capital letters:

    DO NOT USE MAP<k,v> !!!

    the map is keeping references to the imageview (which keeps the activity context) and the bitmaps for ever and it's one of the main reasons you have those memory losses. Further on the reference to context is keep your whole activity in memory, for ever. All very bad ideas.

    You'll use a LruCache (available on the compatibility library) to cache the bitmaps, and let only the activity keep reference to the imageviews (either static from the XML or dynamically using an adapter)

    here is a Google IO video http://www.youtube.com/watch?v=gbQb1PVjfqM where the guys from Google themselves are showing some best practices around this area, on the 4min mark they show the usage of the LruCache.

    0 讨论(0)
  • 2020-12-22 09:13

    // replace this methods in your code and try I am not sure it will solve your error but once you can try.

    private Bitmap getBitmap(String url) 
        {
            //I identify images by hashcode. Not a perfect solution, good for the demo.
            String filename=String.valueOf(url.hashCode());
            File f=new File(cacheDir, filename);
    
            //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=70;
            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.inTempStorage = new byte[32*1024]; 
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }
    

    check this link for the ImageLoaderClass: http://code.google.com/p/vimeoid/source/browse/apk/src/com/fedorvlasov/lazylist/ImageLoader.java

    OOM issue check this link: Strange out of memory issue while loading an image to a Bitmap object

    And if you still get an error then check this one link that I have found after many search and has solved my Problem: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/vYWKY1Y6bUo

    0 讨论(0)
提交回复
热议问题