android获取图库图片并返回

来源:互联网 发布:怎么找淘宝内部优惠券 编辑:程序博客网 时间:2024/06/11 21:51
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();}

原创粉丝点击