java OutOfMemoryError:位图大小超过VM预算
我有ImageView&我的ImageActivity上有一个按钮。单击按钮时,将列出可用图像,单击其中一个图像时,将其加载到ImageView中 imageView xml:java OutOfMemoryError:位图大小超过VM预算,java,android,memory-leaks,bitmap,out-of-memory,Java,Android,Memory Leaks,Bitmap,Out Of Memory,我有ImageView&我的ImageActivity上有一个按钮。单击按钮时,将列出可用图像,单击其中一个图像时,将其加载到ImageView中 imageView xml: <ImageView android:id="@+id/imageView" android:layout_width="300dip" android:layout_height="300dip" android:src="@android:d
<ImageView
android:id="@+id/imageView"
android:layout_width="300dip"
android:layout_height="300dip"
android:src="@android:drawable/alert_light_frame"
/>
问题:问题是,当我单击按钮显示可用图像时,我会选择其中一个&当我在应用程序引发异常的几次重复这些步骤时,它会加载到ImageView中:
java.lang.OutOfMemoryError:位图大小超出VM预算
我盯着它看&发现这个问题是因为内存泄漏,但我无法找到我的代码有什么问题。谁能抽出宝贵的时间来帮助我
谢谢。如果执行一系列的
setImageUri
,则会将一系列位图加载到视图中。如果您正在获取java.lang.OutOfMemoryError:位图大小超出VM预算异常
,这意味着加载新位图时,imageView
不会回收以前的位图。那么你呢
- 或者需要鼓励imageView循环使用以前的位图-可能使用
setImageView(“”)
- 或者从URI中获取位图并使用
;然后可以执行setImageBitmap(android.graphics.bitmap)
和setImageBitmap(null)
bitmap.recycle()
- 很好地调用system.gc()成功了强>
在将图像分配给imageView之前,我调用了System.gc(),以处理内存&它可以工作
请看修改后的代码:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
// calling GC to take care of memory, if you remove this one line of code
// application will throw the error "VM out of memory error"
System.gc();
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
imageView.setImageURI(selectedImageUri);
}
}
}
谢谢
有人能对此发表评论吗?在这种情况下,这种调用GC的方法正确吗?为什么需要保留对imageView的引用?感谢@bestsss,我需要将所选图像保存到SQLite数据库,但是您同时有两个图像,有效地将所需内存增加了一倍。使用前解析图像,而不是一次创建…我没有imageview的源代码,但我假设当您设置ImageUri时,它确实(尝试)加载新图像,然后刷新现有图像,尝试调用
imageview.setBitmap(Bitmap.createImageBitmap(1,1,Bitmap.Config.argb8888));不适合我。bestsss建议使用imageView.setBitmap(Bitmap.createImageBitmap(1,1,Bitmap.Config.ARGB_8888))是您应该鼓励imageView回收的方式(如果您这样做的话)。我尝试了“imageView.setImageBitmap(Bitmap.createBitmap(1,1,Bitmap.Config.ARGB_8888));”但结果是sameI发现了一篇帖子,建议您在设置第二个和后续位图之前必须执行setImageBitmap(null)-请参阅。这说明它是蜂巢式的。你在演什么?
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
// calling GC to take care of memory, if you remove this one line of code
// application will throw the error "VM out of memory error"
System.gc();
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
imageView.setImageURI(selectedImageUri);
}
}
}