1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Android把uri图片转为bitmap

Android把uri图片转为bitmap

时间:2020-08-13 22:23:51

相关推荐

Android把uri图片转为bitmap

直接粘贴就能用

private Bitmap ImageSizeCompress(Uri uri){InputStream Stream = null;InputStream inputStream = null;try {//根据uri获取图片的流inputStream = getContentResolver().openInputStream(uri);BitmapFactory.Options options = new BitmapFactory.Options();//options的in系列的设置了,injustdecodebouond只解析图片的大小,而不加载到内存中去options.inJustDecodeBounds = true;//1.如果通过options.outHeight获取图片的宽高,就必须通过decodestream解析同options赋值//否则options.outheight获取不到宽高BitmapFactory.decodeStream(inputStream,null,options);//2.通过 btm.getHeight()获取图片的宽高就不需要1的解析,我这里采取第一张方式// Bitmap btm = BitmapFactory.decodeStream(inputStream);//以屏幕的宽高进行压缩DisplayMetrics displayMetrics = getResources().getDisplayMetrics();int heightPixels = displayMetrics.heightPixels;int widthPixels = displayMetrics.widthPixels;//获取图片的宽高int outHeight = options.outHeight;int outWidth = options.outWidth;//heightPixels就是要压缩后的图片高度,宽度也一样int a = (int) Math.ceil((outHeight/(float)heightPixels));int b = (int) Math.ceil(outWidth/(float)widthPixels);//比例计算,一般是图片比较大的情况下进行压缩int max = Math.max(a, b);if(max > 1){options.inSampleSize = max;}//解析到内存中去options.inJustDecodeBounds = false;// 根据uri重新获取流,inputstream在解析中发生改变了Stream = getContentResolver().openInputStream(uri);Bitmap bitmap = BitmapFactory.decodeStream(Stream, null, options);return bitmap;} catch (Exception e) {e.printStackTrace();}finally {try {if(inputStream != null) {inputStream.close();}if(Stream != null){Stream.close();}} catch (IOException e) {e.printStackTrace();}}return null;}

引用

Bitmap bitmap = ImageSizeCompress(uri);

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。