inSampleSize
在搜索 SO 并阅读大量答案后,我编写了以下代码来获取对大位图进行下采样的最小值:
public Bitmap load(Context context, String image_url) throws Exception {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image_url, bitmapOptions);
final float imageSize = (float) bitmapOptions.outWidth * (float) bitmapOptions.outHeight * 4.0f / 1024.0f / 1024.0f; // MB
bitmapOptions.inSampleSize = (int) Math.pow(2, Math.floor(imageSize / MemoryManagement.free(context)));
bitmapOptions.inJustDecodeBounds = false;
return BitmapProcessing.modifyOrientation(BitmapFactory.decodeFile(image_url, bitmapOptions), image_url);
}
和
public class MemoryManagement {
public static float free(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int memoryClass = am.getMemoryClass() - 10;
if (memoryClass < 1) memoryClass = 1;
return (float) memoryClass;
}
}
目标是在没有任何 OutOfMemory 异常的情况下获得位图样本的最大尺寸。我可以相信这段代码吗?