public static Bitmap decodeSampledBitmapFromFile(String pathName, int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
Bitmap src = BitmapFactory.decodeFile(pathName, options);
// return createScaleBitmap(src, reqWidth, reqHeight, options.inSampleSize);
return src;
}
private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// 源图片的高度和宽度
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
两种方法的区别
第一种方法获取的bitmap是被缩放的bitmap,第二种方法获取的bitmap是完整的bitmap,实际使用中根据需求情况决定使用哪一种方法。
怎样启动相册获取我们想要的图片
第一步,通过 Intent.ACTION_GET_CONTENT 这个Intent,并设置相应的type,启动相册。
Intent i = new Intent(Intent.ACTION_GET_CONTENT, null);
i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, “image/*”);
startActivityForResult(i, INTENT_CODE_IMAGE_GALLERY1);
第二步,在onActivityResult中对返回的uri数据进行处理
- 需要注意的是:这里我们需要注意是不是MIUI系统,如果不是MIUI系统,我