尝试将位图加载到imageView时java.lang.OutOfMemoryError

时间:2013-10-05 21:19:12

标签: java android eclipse bitmap

我正在尝试将位图加载到android imageView。

enter image description here

但请获取此java.lang.OutOfMemoryError

我该如何避免这种情况?

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)

1 个答案:

答案 0 :(得分:8)

这取决于您的位图有多大,当您在计算机上读取大小时,Android似乎会分配比图像大小更多的空间。所以......

  • 您可以使用较小的图片。
  • 谷歌实际上发布了一个关于避免OutOfMemoryErrors here的指南,这将有很大帮助,尽管我也必须使用较小的图像尺寸。
  • 几乎可以肯定的一种方法是在您的应用标签之间的清单中设置android:largeHeap="true"。这会增加您的堆大小,但可能会使您的应用程序滞后一点。

也许对你而言,这些想法的组合将起作用。