android获取图库图片并返回
16lz
2022-05-28
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 blocke.printStackTrace();}
更多相关文章
- android 按钮的点击缩放
- Android(安卓)图片缩放实例详解
- android 按比例缩放图片(适屏)
- android 方式实现imageview圆角
- ViewPager
- Android(安卓)popupwindow的基本使用
- 最受欢迎的文章汇总
- Android(安卓)调用相册 拍照 实现系统控件缩放 切割图片
- Android图片缩放总结及比较