gpt4 book ai didi

android - 索尼爱立信 Xperia 上的虚拟内存不足

转载 作者:行者123 更新时间:2023-11-30 04:19:24 27 4
gpt4 key购买 nike

我的应用程序包含大量在运行时加载的图像。这个应用程序几乎在所有安卓设备上都能正常工作,但在 Xperia 上它经常因虚拟内存不足而崩溃。请帮助我...

最佳答案

您需要捕获低内存错误并减小下载图像的大小,如下所示。

 catch(OutOfMemoryError e)
{
e.printStackTrace();
Log.e("Out of memory error", e.toString());
reduce_size();
}
.....

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
options.inJustDecodeBounds = true;

Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);

final int REQUIRED_SIZE=70;

int width_tmp=options.outWidth, height_tmp=options.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}

//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
Bitmap btm=BitmapFactory.decodeStream(is, null, o2);
img_t.setImageBitmap(btm);

关于android - 索尼爱立信 Xperia 上的虚拟内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9494554/

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