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

前端 未结 13 2118
醉话见心
醉话见心 2020-11-22 01:32

I developed an application that uses lots of images on Android.

The app runs once, fills the information on the screen (Layouts, Listviews,

13条回答
  •  我寻月下人不归
    2020-11-22 02:31

    FWIW, here's a lightweight bitmap-cache I coded and have used for a few months. It's not all-the-bells-and-whistles, so read the code before you use it.

    /**
     * Lightweight cache for Bitmap objects. 
     * 
     * There is no thread-safety built into this class. 
     * 
     * Note: you may wish to create bitmaps using the application-context, rather than the activity-context. 
     * I believe the activity-context has a reference to the Activity object. 
     * So for as long as the bitmap exists, it will have an indirect link to the activity, 
     * and prevent the garbaage collector from disposing the activity object, leading to memory leaks. 
     */
    public class BitmapCache { 
    
        private Hashtable> hashtable = new Hashtable>();  
    
        private StringBuilder sb = new StringBuilder(); 
    
        public BitmapCache() { 
        } 
    
        /**
         * A Bitmap with the given width and height will be returned. 
         * It is removed from the cache. 
         * 
         * An attempt is made to return the correct config, but for unusual configs (as at 30may13) this might not happen.  
         * 
         * Note that thread-safety is the caller's responsibility. 
         */
        public Bitmap get(int width, int height, Bitmap.Config config) { 
            String key = getKey(width, height, config); 
            ArrayList list = getList(key); 
            int listSize = list.size();
            if (listSize>0) { 
                return list.remove(listSize-1); 
            } else { 
                try { 
                    return Bitmap.createBitmap(width, height, config);
                } catch (RuntimeException e) { 
                    // TODO: Test appendHockeyApp() works. 
                    App.appendHockeyApp("BitmapCache has "+hashtable.size()+":"+listSize+" request "+width+"x"+height); 
                    throw e ; 
                }
            }
        }
    
        /**
         * Puts a Bitmap object into the cache. 
         * 
         * Note that thread-safety is the caller's responsibility. 
         */
        public void put(Bitmap bitmap) { 
            if (bitmap==null) return ; 
            String key = getKey(bitmap); 
            ArrayList list = getList(key); 
            list.add(bitmap); 
        }
    
        private ArrayList getList(String key) {
            ArrayList list = hashtable.get(key);
            if (list==null) { 
                list = new ArrayList(); 
                hashtable.put(key, list); 
            }
            return list;
        } 
    
        private String getKey(Bitmap bitmap) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Config config = bitmap.getConfig();
            return getKey(width, height, config);
        }
    
        private String getKey(int width, int height, Config config) {
            sb.setLength(0); 
            sb.append(width); 
            sb.append("x"); 
            sb.append(height); 
            sb.append(" "); 
            switch (config) {
            case ALPHA_8:
                sb.append("ALPHA_8"); 
                break;
            case ARGB_4444:
                sb.append("ARGB_4444"); 
                break;
            case ARGB_8888:
                sb.append("ARGB_8888"); 
                break;
            case RGB_565:
                sb.append("RGB_565"); 
                break;
            default:
                sb.append("unknown"); 
                break; 
            }
            return sb.toString();
        }
    
    }
    

提交回复
热议问题