80,352
社区成员
发帖
与我相关
我的任务
分享
private Bitmap decodeFile(File file) {
try {
BitmapFactory.Options op = new BitmapFactory.Options();
op.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(file), null, op);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = op.outWidth;
int height_tmp = op.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;
}
BitmapFactory.Options op2 = new BitmapFactory.Options();
op2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(file), null,
op2);
} catch (Exception e) {
e.printStackTrace();
} catch (OutOfMemoryError oom) {
oom.printStackTrace();
}
return null;
}
上面代码中的op2.inSampleSize = scale;就是在调整采样率,以减少内存使用。
当然,也可以通过上面代码的BitmapFactory.Options中的outWidth和outHeight来对尺寸就行修改。