简体   繁体   English

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

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

I'm trying to load a bitmap into an android imageView. 我正在尝试将位图加载到android imageView中。

在此输入图像描述

but get this java.lang.OutOfMemoryError 但得到这个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)

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. 这取决于您的位图有多大,当您在计算机上读取大小时,Android似乎会分配比图像大小更多的空间。 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. 谷歌实际上已经在避免OutOfMemoryError异常出版了一本指南在这里 ,这将有很大的帮助,但我不得不使用较小的图像尺寸为好。
  • One method that will almost definitely work is to set android:largeHeap="true" in your manifest, between your application tags. 几乎可以肯定的一种方法是在清单中,在应用程序标记之间设置android:largeHeap =“true” This will increase your heap size, but may make your app lag a little. 这会增加您的堆大小,但可能会使您的应用程序滞后一点。

Maybe for you, a combination of these ideas will work. 也许对你而言,这些想法的组合将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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