我的应用程序包含许多在运行时加载的图像。这个应用程序在几乎所有Android设备中都能正常工作,但在Xperia中,它通常会因低虚拟内存而崩溃。 请帮助我...
答案 0 :(得分:1)
您需要捕获低内存错误并减小下载图像的大小,如下所示。
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);