我的应用程序包含大量在运行时加载的图像。这个应用程序几乎在所有安卓设备上都能正常工作,但在 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/