gpt4 book ai didi

android - 如何避免内存不足异常

转载 作者:太空宇宙 更新时间:2023-11-03 11:20:34 25 4
gpt4 key购买 nike

在我的应用程序中,我使用图像的 url 从网络上下载图像。我有很多图像,所以我为此实现了分页技术,并且我按垂直顺序为每个页面显示 15 张图像。在这种情况下,我正在向上/向下滚动页面以查看图像,此时我的应用程序崩溃并且出现内存不足异常。请任何人帮助我。

日志:

02-07 11:23:52.256: ERROR/ACRA(7236): El Gifto fatal error : bitmap size exceeds VM budget(Heap Size=7943KB, Allocated=3485KB, Bitmap Size=12546KB)    
02-07 11:23:52.256: ERROR/ACRA(7236): java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=7943KB, Allocated=3485KB, Bitmap Size=12546KB)
02-07 11:23:52.256: ERROR/ACRA(7236): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-07 11:23:52.256: ERROR/ACRA(7236): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:690)
02-07 11:23:52.256: ERROR/ACRA(7236): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:490)
02-07 11:23:52.256: ERROR/ACRA(7236): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-07 11:23:52.256: ERROR/ACRA(7236): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
02-07 11:23:52.256: ERROR/ACRA(7236): at com.ibkr.elgifto.GiftCategories$itemlistadapter$3.getDrawableFromUrl(GiftCategories.java:837)
02-07 11:23:52.256: ERROR/ACRA(7236): at com.ibkr.elgifto.GiftCategories$itemlistadapter$3.run(GiftCategories.java:724)

这是我的代码:

public void DownLoadImageInAThreadHandler(final CategoryData Item, final ViewHolder holder)
{
final Handler handler = new Handler()
{
@Override public void handleMessage(Message message)
{
holder.imgitem.setImageDrawable((Drawable) message.obj);
holder.imgitem.setVisibility(View.VISIBLE);
holder.progress.setVisibility(View.GONE);
}
};

//Thread for getting the attributes values
Thread t = new Thread()
{
public void run()
{
try
{
drawable = getDrawableFromUrl(Item.ImageUrl);

if(drawable != null)
{
//Send the message to the handler
Message message = handler.obtainMessage(1, drawable);
handler.sendMessage(message);
}
else
{
int idNoImage = R.drawable.giftsuggestionsnoimage;
Drawable dwgNoImg = GiftCategories.this.getResources().getDrawable(idNoImage);

//Send the message to the handler
Message message = handler.obtainMessage(1, dwgNoImg);
handler.sendMessage(message);
}

}
catch(Exception exp)
{
System.out.println("Exception in DownLoadImageInAThread : " + exp.getMessage());
}
}

private Drawable getDrawableFromUrl(String imageUrl) throws IOException
{
Drawable image = null;

try
{
InputStream in = (java.io.InputStream) new java.net.URL(imageUrl).getContent();
if (in != null)
{
image = Drawable.createFromStream(in, "image");
in.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
return image;
}
};
t.start();
}

最佳答案

如果你的内存有问题,那么你必须有两个选择 首先。=>你必须使用位图解码图像 或 Second => 下载图像并将其保存在临时文件夹中并使用它。当您不需要从文件夹中删除所有文件时。

用于下载图像并将其保存在文件夹中 check this link

用于解码 Check this link from stackoverflow .也许这会对你有所帮助。谢谢

关于android - 如何避免内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9171857/

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