Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.OutOfMemoryError when trying to load a bitmap into imageView

I'm trying to load a bitmap into an android imageView.

enter image description here

but get this java.lang.OutOfMemoryError

how can I avoid this ?

10-06 00:11:21.225: E/AndroidRuntime(6825): FATAL EXCEPTION: main
10-06 00:11:21.225: E/AndroidRuntime(6825): java.lang.OutOfMemoryError
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:722)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:790)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at de.vogella.android.todos.MySimpleArrayAdapter.getView(MySimpleArrayAdapter.java:85)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.widget.AbsListView.obtainView(AbsListView.java:2452)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1250)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.widget.ListView.onMeasure(ListView.java:1162)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.view.View.measure(View.java:15481)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5107)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-06 00:11:21.225: E/AndroidRuntime(6825):     at android.view.View.measure(View.java:15481)
like image 740
Elad Benda Avatar asked Oct 05 '13 21:10

Elad Benda


1 Answers

It depends how big your bitmap is, Android seems to allocate a little more space than the size of the image when you read the size on your computer. So...

  • You could just use a smaller image.
  • Google have actually published a guide on avoiding OutOfMemoryErrors here which will help a lot, though I had to use a smaller image size as well.
  • One method that will almost definitely work is to set android:largeHeap="true" in your manifest, between your application tags. This will increase your heap size, but may make your app lag a little.

Maybe for you, a combination of these ideas will work.

like image 86
roarster Avatar answered Nov 17 '22 04:11

roarster