简体   繁体   中英

java.lang.OutOfMemoryError: bitmap size exceeds VM budget

I'm using a BitmapFactory to decode a JPG from file and then setting the imageBitmap of an ImageView with that. Sometimes, the app crashes with " java.lang.OutOfMemoryError: bitmap size exceeds VM budget "

I've tried using:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeFile(filepath, options);

it seemed to be working for a while, but not anymore. What can I do?

In Android, there is a cap on how much memory that can be allocated to an application, it could be around 16 MB to 32 MB. (Upto Android Version)

For a 2 Mega Pixel, memory required for the bitmap would be 2 * 4 = 8 M.

To reduce memory consumption, you need to lower the image resolution. Say WVGA, 800x480 to start with

Shash

Reduce the number of images you use, or cache them, and if you cache them, store them in the minimum size you need.

Regards, Stéphane

And this is the code to cache image files.

protected static Map<String, Drawable> cacheThumbs = new HashMap<String, Drawable>();

//code to cache images

                if( thumbUri.length() == 0 )
                    return;
                Drawable d = cacheThumbs.get( thumbUri );
                if( d == null )
                {
                    String fileName = movie.getId()+"_"+thumbUri.substring( thumbUri.lastIndexOf('/')+1 );
                    cacheFile = new File( application.getCacheDir(), fileName );
                    if( !cacheFile.exists() )
                    {
                        //Log.v(LOG_TAG,"Fetching "+thumbUri +" and caching in "+cacheFile.getAbsolutePath() );
                        InputStream bis = (InputStream) new URL(thumbUri).getContent();
                        BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream( cacheFile ) );
                        int read = 0;
                        while( (read = bis.read( buffer )) != -1 )
                            bos.write( buffer, 0, read );
                        bis.close();
                        bos.close();                         
                        Log.v(LOG_TAG,thumbUri + " fetched");
                    }//if
                    Log.v(LOG_TAG,thumbUri + " in cache");
                    BufferedInputStream bis = new BufferedInputStream( new FileInputStream( cacheFile ), 9000 );
                    d = Drawable.createFromStream( bis, "src name");
                    bis.close();
                    cacheThumbs.put( thumbUri, d );

Regards, Stéphane

I would like to add a different view to this discussion. I did read so many threads and suggestions and ended up with a WebView to display single images. It's not ment for galleries just for a single image. It's working perfect, its zoomable by options, its scrollable and finally no memory errors ;-)

It works for images you load from the web and for local images as well. Here's a small example:

WebView webView = (WebView) findViewById(R.id.your_webview);

String html = "<html><head><title>A title</title></head><body bgcolor=\"#000000\"><img src=\"" + url + "\"></body></html>";

WebSettings webSettings = webView.getSettings();
webSettings.setBuiltInZoomControls(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);   
webSettings.setUseWideViewPort(true);

webView.setScrollbarFadingEnabled(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.loadDataWithBaseURL("fake://fake.com", html, "text/html", "UTF-8", null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM