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();}

更多相关文章

  1. Android圆角图片封装类(直接使用)
  2. Android 图片缩放实例详解
  3. Android 自定义文本框(带图片)
  4. android 按钮的点击缩放
  5. Android Fresco图片处理库用法API英文原文文档3(Facebook开源Andr
  6. android 多张图片动画方式轮播(转载)
  7. Android 改变ImageView图片的Bitmap大小
  8. Android通过内容提供器获取相册中所有图片
  9. Android绘制圆形图片(五)

随机推荐

  1. Android(安卓)开发手记之NDK 编程实例
  2. Android(安卓)核心分析 之七------Servic
  3. Android源代码分析(三) MediaScanner源码
  4. Android(安卓)wps文件下载、预览
  5. Google Android操作系统内核编译图文教程
  6. [Android进阶]Android消息机制
  7. Android中的网络时间同步
  8. Android(安卓)FrameWork学习(二)Android系
  9. 创建 Android(安卓)上使用的自签名证书(Cr
  10. Android客户端与PC服务器实现Socket通信(