gpt4 book ai didi

android - 如何处理内存不足错误?

转载 作者:太空狗 更新时间:2023-10-29 15:27:57 26 4
gpt4 key购买 nike

我正在使用以下代码在我的 ImageView 中显示位图。当我尝试加载尺寸大于 1.5MB 的图像时,它给我错误。有人建议我解决方案吗?

  try {  

URL aURL = new URL(myRemoteImages[val]);
URLConnection conn = aURL.openConnection();

conn.connect();
InputStream is = null;
try
{
is= conn.getInputStream();
}catch(IOException e)
{


return 0;

}
int a= conn.getConnectTimeout();
BufferedInputStream bis = new BufferedInputStream(is);

Bitmap bm;
try
{
bm = BitmapFactory.decodeStream(bis);
}catch(Exception ex)
{
bis.close();
is.close();
return 0;
}
bis.close();
is.close();
img.setImageBitmap(bm);

} catch (IOException e) {
return 0;
}

return 1;

日志猫:

06-14 12:03:11.701: ERROR/AndroidRuntime(443): Uncaught handler: thread main exiting due to uncaught exception
06-14 12:03:11.861: ERROR/AndroidRuntime(443): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-14 12:03:11.861: ERROR/AndroidRuntime(443): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)

最佳答案

您应该使用 inSampleSize 选项进行解码以减少内存消耗。 Strange out of memory issue while loading an image to a Bitmap object

JustDecodeBounds 中的另一个选项可以帮助您找到正确的 inSampleSize 值 http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a

关于android - 如何处理内存不足错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3035228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com