0

I am using following code to get bitmap from url. This function is used multiple times in my code and on third or fourth call crashes showing following error. Url exists and I am sure that there is no difference between previous calls. Thank you

 public static Bitmap getBitmapFromURL(String src) {
                try {
                    Log.e("src",src);
                    URL url = new URL(src);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoInput(true);
                    connection.connect();
                    InputStream input = connection.getInputStream();
                    Bitmap myBitmap = BitmapFactory.decodeStream(input); //crashes at this line
                    Log.e("Bitmap","returned");
                    return myBitmap;
                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e("Exception",e.getMessage());
                    return null;
                }
            }

06-15 14:15:34.365: E/AndroidRuntime(863): FATAL EXCEPTION: main
06-15 14:15:34.365: E/AndroidRuntime(863): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-15 14:15:34.365: E/AndroidRuntime(863):  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-15 14:15:34.365: E/AndroidRuntime(863):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
06-15 14:15:34.365: E/AndroidRuntime(863):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:515)
06-15 14:15:34.365: E/AndroidRuntime(863):  at com.nimbosolutions.projecte1.TabBar_iOSActivity.getBitmapFromURL(TabBar_iOSActivity.java:232)
06-15 14:15:34.365: E/AndroidRuntime(863):  at com.nimbosolutions.projecte1.TabBar_iOSActivity.addTab(TabBar_iOSActivity.java:209)
Jaume
  • 3,672
  • 19
  • 60
  • 119
  • 1
    have you [Googled](https://www.google.co.in/search?sugexp=chrome,mod=15&ix=h9&sourceid=chrome&ie=UTF-8&q=android+bitmap+size+exceed+VM+budget+error) before asking question? – Samir Mangroliya Jun 15 '12 at 14:26
  • yes I did, that is why I am asking it! – Jaume Jun 15 '12 at 14:47
  • possible duplicate of [OutOfMemoryError: bitmap size exceeds VM budget :- Android](http://stackoverflow.com/questions/2928002/outofmemoryerror-bitmap-size-exceeds-vm-budget-android) – cHao Jun 17 '12 at 03:29

1 Answers1

0

You said "third or fourth time" - is it the same bitmap every time? If yes, why not cache and reuse?

If no, do you really need all 3-4 bitmaps in memory at the same time? If yes, you're screvved. If not, may I recommend that you call recycle() on your bitmaps before you discard them (i. e. before you null the reference)? This is known to free up memory faster than vanilla GC would.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • not the same bitmap. I do not need to 3-4 bitmaps in memory, just one. Do you mean recycle() and then input = null? – Jaume Jun 15 '12 at 14:47
  • No, the stream is OK (although closing explicitly is not a bad idea). The Bitmap object is the one that needs recycling. It's up to the caller, the one that receives `myBitmap`. – Seva Alekseyev Jun 15 '12 at 14:57