参考自《第一行代码》及相关资料,这里存一遍后以后就不用再重头打一遍了。。

首先由于进行读写操作,要在 AndroidManifest.xml中声明权限:

[java] view plain copyprint?
  1. <uses-permission android:name=“android.permission.READ_EXTERNAL_STORAGE” />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

调用系统相册:

[java] view plain copyprint?
  1. private static final int CHOOSE_PHOTO=0;
  2. Intent intent = new Intent(“android.intent.action.GET_CONTENT”);
  3. intent.setType(”image/*”);
  4. startActivityForResult(intent, CHOOSE_PHOTO);
private static final int CHOOSE_PHOTO=0;
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, CHOOSE_PHOTO);

然后回调:

[java] view plain copyprint?
  1. @Override
  2. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  3. switch (requestCode) {
  4. case CHOOSE_PHOTO:
  5. if (resultCode == RESULT_OK) {
  6. Bitmap bitmap = null;
  7. //判断手机系统版本号
  8. if (Build.VERSION.SDK_INT >= 19) {
  9. //4.4及以上系统使用这个方法处理图片
  10. bitmap = ImgUtil.handleImageOnKitKat(this, data);        //ImgUtil是自己实现的一个工具类
  11. } else {
  12. //4.4以下系统使用这个方法处理图片
  13. bitmap = ImgUtil.handleImageBeforeKitKat(this, data);
  14. }
  15. ImageView view = (ImageView) findViewById(R.id.personal_info_header_img);
  16. view.setImageBitmap(bitmap);
  17. }
  18. break;
  19. default:
  20. break;
  21. }
  22. }
 @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {case CHOOSE_PHOTO:if (resultCode == RESULT_OK) {Bitmap bitmap = null;//判断手机系统版本号if (Build.VERSION.SDK_INT >= 19) {//4.4及以上系统使用这个方法处理图片bitmap = ImgUtil.handleImageOnKitKat(this, data);        //ImgUtil是自己实现的一个工具类} else {//4.4以下系统使用这个方法处理图片bitmap = ImgUtil.handleImageBeforeKitKat(this, data);}ImageView view = (ImageView) findViewById(R.id.personal_info_header_img);view.setImageBitmap(bitmap);}break;default:break;}}

将对图像的相关操作封装成一个ImgUtil类,便于使用:

[java] view plain copyprint?
  1. import android.annotation.TargetApi;
  2. import android.content.ContentUris;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.SharedPreferences;
  6. import android.database.Cursor;
  7. import android.graphics.Bitmap;
  8. import android.graphics.BitmapFactory;
  9. import android.net.Uri;
  10. import android.preference.PreferenceManager;
  11. import android.provider.DocumentsContract;
  12. import android.provider.MediaStore;
  13. import android.text.TextUtils;
  14. import java.io.ByteArrayInputStream;
  15. import java.io.ByteArrayOutputStream;
  16. /**
  17. * Created by wbin on 2016/3/22.
  18. */
  19. public class ImgUtil {
  20. //4.4及以上系统使用这个方法处理图片
  21. @TargetApi(19)
  22. public static Bitmap handleImageOnKitKat(Context context, Intent data) {
  23. String imagePath = null;
  24. Uri uri = data.getData();
  25. if (DocumentsContract.isDocumentUri(context, uri)) {
  26. //如果是document类型的Uri,则通过document id处理
  27. String docId = DocumentsContract.getDocumentId(uri);
  28. if (“com.android.providers.media.documents”.equals(uri.getAuthority())) {
  29. String id = docId.split(”:”)[1];  //解析出数字格式的id
  30. String selection = MediaStore.Images.Media._ID + ”=” + id;
  31. imagePath = getImagePath(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
  32. } else if (“com.android.providers.downloads.documents”.equals(uri.getAuthority())) {
  33. Uri contentUri = ContentUris.withAppendedId(
  34. Uri.parse(”content://downloads/public_downloads”), Long.valueOf(docId));
  35. imagePath = getImagePath(context, contentUri, null);
  36. }
  37. } else if (“content”.equalsIgnoreCase(uri.getScheme())) {
  38. //如果不是document类型的Uri,则使用普通方式处理
  39. imagePath = getImagePath(context, uri, null);
  40. }
  41. return getImage(imagePath);
  42. }
  43. //4.4以下系统使用这个方法处理图片
  44. public static Bitmap handleImageBeforeKitKat(Context context, Intent data) {
  45. Uri uri = data.getData();
  46. String imagePath = getImagePath(context, uri, null);
  47. return getImage(imagePath);
  48. }
  49. public static String getImagePath(Context context, Uri uri, String selection) {
  50. String path = null;
  51. //通过Uri和selection来获取真实的图片路径
  52. Cursor cursor = context.getContentResolver().query(uri, null, selection, null, null);
  53. if (cursor != null) {
  54. if (cursor.moveToFirst()) {
  55. path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
  56. }
  57. cursor.close();
  58. }
  59. return path;
  60. }
  61. //对bitmap进行质量压缩
  62. public static Bitmap compressImage(Bitmap image) {
  63. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  64. image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
  65. int options = 100;
  66. while (baos.toByteArray().length / 1024 > 100) {    //循环判断如果压缩后图片是否大于100kb,大于继续压缩
  67. baos.reset();//重置baos即清空baos
  68. image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
  69. options -= 10;//每次都减少10
  70. }
  71. ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
  72. Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
  73. return bitmap;
  74. }
  75. //传入图片路径,返回压缩后的bitmap
  76. public static Bitmap getImage(String srcPath) {
  77. if (TextUtils.isEmpty(srcPath))  //如果图片路径为空 直接返回
  78. return null;
  79. BitmapFactory.Options newOpts = new BitmapFactory.Options();
  80. //开始读入图片,此时把options.inJustDecodeBounds 设回true了
  81. newOpts.inJustDecodeBounds = true;
  82. Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此时返回bm为空
  83. newOpts.inJustDecodeBounds = false;
  84. int w = newOpts.outWidth;
  85. int h = newOpts.outHeight;
  86. //现在主流手机比较多是800*480分辨率,所以高和宽我们设置为
  87. float hh = 800f;//这里设置高度为800f
  88. float ww = 480f;//这里设置宽度为480f
  89. //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
  90. int be = 1;//be=1表示不缩放
  91. if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
  92. be = (int) (newOpts.outWidth / ww);
  93. } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
  94. be = (int) (newOpts.outHeight / hh);
  95. }
  96. if (be <= 0)
  97. be = 1;
  98. newOpts.inSampleSize = be;//设置缩放比例
  99. //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了
  100. bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
  101. return compressImage(bitmap);//压缩好比例大小后再进行质量压缩
  102. }
  103. }
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;/*** Created by wbin on 2016/3/22.*/
public class ImgUtil {//4.4及以上系统使用这个方法处理图片@TargetApi(19)public static Bitmap handleImageOnKitKat(Context context, Intent data) {String imagePath = null;Uri uri = data.getData();if (DocumentsContract.isDocumentUri(context, uri)) {//如果是document类型的Uri,则通过document id处理String docId = DocumentsContract.getDocumentId(uri);if ("com.android.providers.media.documents".equals(uri.getAuthority())) {String id = docId.split(":")[1];  //解析出数字格式的idString selection = MediaStore.Images.Media._ID + "=" + id;imagePath = getImagePath(context, MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));imagePath = getImagePath(context, contentUri, null);}} else if ("content".equalsIgnoreCase(uri.getScheme())) {//如果不是document类型的Uri,则使用普通方式处理imagePath = getImagePath(context, uri, null);}return getImage(imagePath);}//4.4以下系统使用这个方法处理图片public static Bitmap handleImageBeforeKitKat(Context context, Intent data) {Uri uri = data.getData();String imagePath = getImagePath(context, uri, null);return getImage(imagePath);}public static String getImagePath(Context context, Uri uri, String selection) {String path = null;//通过Uri和selection来获取真实的图片路径Cursor cursor = context.getContentResolver().query(uri, null, selection, null, null);if (cursor != null) {if (cursor.moveToFirst()) {path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));}cursor.close();}return path;}//对bitmap进行质量压缩public static Bitmap compressImage(Bitmap image) {ByteArrayOutputStream baos = new ByteArrayOutputStream();image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中int options = 100;while (baos.toByteArray().length / 1024 > 100) {    //循环判断如果压缩后图片是否大于100kb,大于继续压缩baos.reset();//重置baos即清空baosimage.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中options -= 10;//每次都减少10}ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片return bitmap;}//传入图片路径,返回压缩后的bitmappublic static Bitmap getImage(String srcPath) {if (TextUtils.isEmpty(srcPath))  //如果图片路径为空 直接返回return null;BitmapFactory.Options newOpts = new BitmapFactory.Options();//开始读入图片,此时把options.inJustDecodeBounds 设回true了newOpts.inJustDecodeBounds = true;Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//此时返回bm为空newOpts.inJustDecodeBounds = false;int w = newOpts.outWidth;int h = newOpts.outHeight;//现在主流手机比较多是800*480分辨率,所以高和宽我们设置为float hh = 800f;//这里设置高度为800ffloat ww = 480f;//这里设置宽度为480f//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可int be = 1;//be=1表示不缩放if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放be = (int) (newOpts.outWidth / ww);} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放be = (int) (newOpts.outHeight / hh);}if (be <= 0)be = 1;newOpts.inSampleSize = be;//设置缩放比例//重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了bitmap = BitmapFactory.decodeFile(srcPath, newOpts);return compressImage(bitmap);//压缩好比例大小后再进行质量压缩}
}

为了兼容新老版本的手机,我们做了一个判断,如果是4.4及以上系统的手机就调用handleImageOnKitKat()方法来处理图片,否则就调用handleImageBeforeKitKat()方法来处理图片。之所以要这么做,是因为Android系统从4.4版本开始,选取相册的图片不再返回图片真是的Uri了,而是一个封装过的Uri,因此如果是4.4版本以上的手机需要对这个Uri进行解析才行。

当然了,获取到图片路径后不推荐直接使用 BitmapFactory.decodeFile(imgPath)来获取bitmap,因为某些图片体积可能很大,直接加载到内存中有可能会导致程序崩溃(我就遇到过了..你可以直接加载手机高像素拍的原图片试试看=。=)。 所以更好的做法是先对图片进行适当的压缩,然后再加载到内存中(上述代码中实现了)。

好了,以后需要直接来这复制就行了,不用再蛋疼打这么多了=。=  懒人福利。

转自wbin233的博客:http://blog.csdn.net/wbin233/article/details/50954868#comments

Android 从相册中选择照片并返回相关推荐

  1. android点击选择相册,android: 从相册中选择照片

    虽然调用摄像头拍照既方便又快捷,但并不是每一次我们都需要去当场拍一张照片的. 因为每个人的手机相册里应该都会存有许许多多张照片,直接从相册里选取一张现有的照 片会比打开相机拍一张照片更加常用.一个优秀 ...

  2. Android --- 从相册中选择图片或者拍着选择图片遇到的问题

    1.从相册选择的图片会存储到 data 里面,而相机拍照的图片不会存储到 data 里面 2.由于从相册选择的图片会存储到 data 里面,而相机拍照的图片不会存储到 data 里面,所以在获取图片资 ...

  3. 关于解决 从相册中选择照片后无法剪切图片以及无法加载图片的问题

    程序分析: 主要分两个部分 (1)先拍照,然后剪切,最后显示出来.    (2)从相册中选择一张照片进行剪切然后显示出来 代码参照<第一行代码>,书中的代码运行时(2)出现了问题,无法剪切 ...

  4. 打开相机拍照或从相册中选择照片

    1.添加权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ...

  5. 坑:Android从相册中选择图片加载到ImageView中显示不出

  6. Android学习-运用手机多媒体-通知使用技巧、调用摄像头拍照、从相册中选取照片、播放音频和视频文件(MediaPlayer、VideoView)

    android提供了一系列的API,使得我们可以在程序中调用很多的多媒体资源,从而编写出更加丰富多彩的应用程序. 使用通知 通知(Notification)是Android系统中比较有特色的一个功能, ...

  7. Android 拍照以及相册中选择(适配高版本)————上传多张照片之压缩处理、在线预览可伸缩放大(二)

    ______ Introduction ______ 前言 上一篇文章刚给大家总结完,关于上传头像的功能.此文章所述 主要是关于上传头像的具体流程以及如何对照片做裁剪处理,回调给控件显示:当然重中之重 ...

  8. 兼容Android 11 相机拍照,从相册中选择,裁剪图片

    由于android 11对存储空间进行了更新,导致无法进入裁剪或者裁剪后无法保存,返回路径等问题. android 10以下可以参考:android 相机拍照,从相册中选择,裁剪图片 前面部分和之前的 ...

  9. Android中拍照(相册中选择)并上传图片功能(包括动态获取权限)

    作为新手小白,为了实现这个拍照和相册选取图片并上传功能,确实花费了很多时间,因为实现不容易,所以记录下来,一和大家分享,二为之后学习做个备忘. 一.实现效果 二. 整体思路 Android手机客户端, ...

最新文章

  1. [书目20130216]深入浅出WPF
  2. 区块链只能炒币?5G通信、医疗健康、食品安全,采购管理这些领域都已用上国产自研长安链...
  3. maven 与intellij IDEA 下maven 为groovy 项目生成jar 详解
  4. 细数家庭安防五大乱象 何时能步入正轨
  5. 微服务java模块内存管理_Java 9模块服务
  6. Git 忽略编译后文件
  7. 【计算机网络】计算机网络体系结构
  8. adafruit1306_Adafruit的2015年最佳开源可穿戴设备
  9. windows上配置新的conda env+torch
  10. 取消endnotes参考文献格式域的步骤_大学体悟-毕业论文格式篇
  11. 关于flash跨域问题
  12. libtorrent源码分析(二)VS上libtorrent编译总结
  13. 台式计算机是32位还64位,电脑系统装64位还是32位系统好?考虑这些才能正确安装...
  14. 大动作,天津农学院专升本停招了吗
  15. Handler同步屏障
  16. 皮带撕裂检测matlab,基于机器视觉的皮带纵向撕裂检测方法
  17. Rockchip Linux CMA开发文档
  18. 现代控制理论——状态空间表达式的能控性标准型和能观性标准型
  19. 信奥一本通-动态规划-例9.2-数字金字塔-方法四-逆推法代码实现
  20. 编写ringBuff过程中遇到的一次bug

热门文章

  1. 2017 java 面试大全
  2. 【考研英语语法】形容词副词的比较级最高级练习题
  3. pg数据库numeric对应oracle,Oracle类型number与PG类型numeric对比和转换策略
  4. 关于MacBook蓝牙键盘鼠标耳机音响等设备各种的卡顿问题
  5. 前后端实现Base64格式的传参及转换
  6. minio对象存储单机部署并设置开机自启动及集成spring boot进行(创建删除桶)(上传下载删除文件)
  7. 数据脱敏的场景与价值【总结】
  8. 如何查看存款和贷款的基准利率
  9. php 生成 webp,使用PHP脚本批量转换webp文件为png文件
  10. win10 + Ubuntu 18.04.1 LTS (Bionic Beaver) 双系统的安装配置