Uri uri = data.getData();
/*
* 此时会抛出异常
* Uri uri = data.getData();
InputStream isl = getContentResolver().openInputStream(uri);
Bitmap b = BitmapFactory.decodeStream(isl);*/
try {
InputStream is = getContentResolver().openInputStream(uri);
BitmapFactory.Options opts = new Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, opts);
//图片宽高
int bitmapHeight = opts.outHeight;
int bitmapWidth = opts.outWidth;
int windowHeight = wm.getDefaultDisplay().getHeight();
int windowWidth = wm.getDefaultDisplay().getWidth();
//需要缩放
if(bitmapHeight>windowHeight || bitmapWidth>windowWidth){
int scaleX = bitmapWidth/windowWidth;
int scaleY = bitmapHeight/windowHeight;
if(scaleX>scaleY){//按照水平方向缩放
opts.inSampleSize = scaleX;
} else {//按照竖直方向缩放
opts.inSampleSize = scaleY;
}
} else {//不用缩放
opts.inSampleSize = 1;
}
//真正的解析图片
opts.inJustDecodeBounds = false;
//需要注意的事情 is先前已经用过 后面还要继续用 故需要重新获取
is = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(is, null, opts);
ib_main_logo.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
android获取图库图片并返回
最新推荐文章于 2021-05-26 03:35:19 发布