1

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?

1
  • what is the image resolution? Commented Aug 8, 2011 at 15:26

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

so right now, i have the Camera.Parameters set to setJpegQuality(100). I guess I need to change that?
0

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

2 Comments

How do I store them in a particular size?
0

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

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.