我一般都是这样啦..
try
{
mBitmap=BitmapFactory.decodeFile(resPath);
} catch (OutOfMemoryError e)
{
mBitmap=createFitinBitmap(resPath,SCREEWIDTH,SCREENHEIGHT);
}
public Bitmap createFitinBitmap(String path, int fitinWidth, int fitinHeight)
{
Options opts = new Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opts);
int sampleSize1 = opts.outWidth / fitinWidth;
int sampleSize2 = opts.outHeight / fitinHeight;
opts.inSampleSize = sampleSize1>sampleSize2? sampleSize1 : sampleSize2;
opts.inJustDecodeBounds = false;
opts.inDither = false;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
return BitmapFactory.decodeFile(path, opts);
}
本文介绍了一种在遇到内存溢出错误时优化图片加载的方法。通过使用BitmapFactory解码图片并调整采样率来减少内存消耗,确保应用程序的稳定运行。

被折叠的 条评论
为什么被折叠?



