Out of memory cache error when accessing inside the app

后端 未结 2 650
野趣味
野趣味 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: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

    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

提交回复
热议问题