两个问题:
1. 通过调用系统Action,从图库中选择图片,并展示到界面上
2. API19前后获取相册图片路径
详见(仅客户端代码),请移步:本人GITHUB

Intent intent = new Intent();
// Set an explicit MIME data type.
intent.setType("image/*");
// Set the general action to be performed.
intent.setAction(Intent.ACTION_GET_CONTENT);
// callBack
startActivityForResult(intent,1);
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent data) {if (resultCode == RESULT_OK) {Uri uri = data.getData();ContentResolver cr =this.getContentResolver();try {Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));/* 将Bitmap设定到ImageView */iv_local_pic.setImageBitmap(bitmap);int sdkVersion = Integer.valueOf(android.os.Build.VERSION.SDK);Log.d("sdkVersion:", String.valueOf(sdkVersion));Log.d("KITKAT:", String.valueOf(Build.VERSION_CODES.KITKAT));if (sdkVersion >= 19) {  // 或者 android.os.Build.VERSION_CODES.KITKAT这个常量的值是19path = uri.getPath();//5.0直接返回的是图片路径 Uri.getPath is :  /document/image:46 ,5.0以下是一个和数据库有关的索引值System.out.println("path:" + path);// path_above19:/storage/emulated/0/girl.jpg 这里才是获取的图片的真实路径path = getPath_above19(Upload_HttpUrlConnection_Activity.this, uri);System.out.println("path_above19:" + path);} else {path = getFilePath_below19(uri);}} catch (FileNotFoundException e) {Log.e("Exception", e.getMessage(), e);}}super.onActivityResult(requestCode, resultCode, data);}
    /*** API19以下获取图片路径的方法* @param uri*/private String getFilePath_below19(Uri uri) {//这里开始的第二部分,获取图片的路径:低版本的是没问题的,但是sdk>19会获取不到String[] proj = {MediaStore.Images.Media.DATA};//好像是android多媒体数据库的封装接口,具体的看Android文档Cursor cursor = getContentResolver().query(uri, proj, null, null, null);//获得用户选择的图片的索引值int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);System.out.println("***************" + column_index);//将光标移至开头 ,这个很重要,不小心很容易引起越界cursor.moveToFirst();//最后根据索引值获取图片路径   结果类似:/mnt/sdcard/DCIM/Camera/IMG_20151124_013332.jpgString path = cursor.getString(column_index);System.out.println("path:" + path);return path;}
/*** APIlevel 19以上才有* 创建项目时,我们设置了最低版本API Level,比如我的是10,* 因此,AS检查我调用的API后,发现版本号不能向低版本兼容,* 比如我用的“DocumentsContract.isDocumentUri(context, uri)”是Level 19 以上才有的,* 自然超过了10,所以提示错误。* 添加    @TargetApi(Build.VERSION_CODES.KITKAT)即可。** @param context* @param uri* @return*/@TargetApi(Build.VERSION_CODES.KITKAT)public  static String getPath_above19(final Context context, final Uri uri) {final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;// DocumentProviderif (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {// ExternalStorageProviderif (isExternalStorageDocument(uri)) {final String docId = DocumentsContract.getDocumentId(uri);final String[] split = docId.split(":");final String type = split[0];if ("primary".equalsIgnoreCase(type)) {return Environment.getExternalStorageDirectory() + "/" + split[1];}// TODO handle non-primary volumes}// DownloadsProviderelse if (isDownloadsDocument(uri)) {final String id = DocumentsContract.getDocumentId(uri);final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));return getDataColumn(context, contentUri, null, null);}// MediaProviderelse if (isMediaDocument(uri)) {final String docId = DocumentsContract.getDocumentId(uri);final String[] split = docId.split(":");final String type = split[0];Uri contentUri = null;if ("image".equals(type)) {contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;} else if ("video".equals(type)) {contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;} else if ("audio".equals(type)) {contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;}final String selection = "_id=?";final String[] selectionArgs = new String[]{split[1]};return getDataColumn(context, contentUri, selection, selectionArgs);}}// MediaStore (and general)else if ("content".equalsIgnoreCase(uri.getScheme())) {// Return the remote addressif (isGooglePhotosUri(uri))return uri.getLastPathSegment();return getDataColumn(context, uri, null, null);}// Fileelse if ("file".equalsIgnoreCase(uri.getScheme())) {return uri.getPath();}return null;}/*** Get the value of the data column for this Uri. This is useful for* MediaStore Uris, and other file-based ContentProviders.** @param context       The context.* @param uri           The Uri to query.* @param selection     (Optional) Filter used in the query.* @param selectionArgs (Optional) Selection arguments used in the query.* @return The value of the _data column, which is typically a file path.*/public static String getDataColumn(Context context, Uri uri, String selection,String[] selectionArgs) {Cursor cursor = null;final String column = "_data";final String[] projection = {column};try {cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,null);if (cursor != null && cursor.moveToFirst()) {final int index = cursor.getColumnIndexOrThrow(column);return cursor.getString(index);}} finally {if (cursor != null)cursor.close();}return null;}/*** @param uri The Uri to check.* @return Whether the Uri authority is ExternalStorageProvider.*/public static boolean isExternalStorageDocument(Uri uri) {return "com.android.externalstorage.documents".equals(uri.getAuthority());}/*** @param uri The Uri to check.* @return Whether the Uri authority is DownloadsProvider.*/public static boolean isDownloadsDocument(Uri uri) {return "com.android.providers.downloads.documents".equals(uri.getAuthority());}/*** @param uri The Uri to check.* @return Whether the Uri authority is MediaProvider.*/public static boolean isMediaDocument(Uri uri) {return "com.android.providers.media.documents".equals(uri.getAuthority());}/*** @param uri The Uri to check.* @return Whether the Uri authority is Google Photos.*/public static boolean isGooglePhotosUri(Uri uri) {return "com.google.android.apps.photos.content".equals(uri.getAuthority());}

Android获取相册中图片的路径 4.4版本前后的变化相关推荐

  1. Android 获取相册全部图片

    异步加载相册全部图片到项目里,这里我用到了Loader去加载图片.Loader机制是在Android3.0以后引入进来的一个非常好用的异步加载机制.Loader运行在一个单独的线程中,不会阻塞UI线程 ...

  2. android 相册分组,Android获取相册路径

    Android获取相册路径 (2015-10-14 16:14:38) 标签: android 开发 源代码 freetheory 相册路径 private void getPhotoThumbnai ...

  3. 获取IPhone相册中图片的方法(包括获取所有图片)

    获取iphone相册方法: 方法一: ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *gr ...

  4. 获取IPhone相册中图片的方法(ASSets)

    获取iphone相册方法: 方法一: ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *gr ...

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

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

  6. android+获取相册列表,android 获取相册列表的实现(一)

    该项目实现的功能如下: 获取手机相册,点击每个相册之后进入该相册的图片列表界面,在图片列表界面可以实现图片多选,然后进入所选择的图片界面,在该界面内可以实现所选图片的上传等功能. 该项目最大特色: 1 ...

  7. android+获取相册列表,android 获取相册列表的实现(二)

    该项目实现的功能如下: 获取手机相册,点击每个相册之后进入该相册的图片列表界面,在图片列表界面可以实现图片多选,然后进入所选择的图片界面,在该界面内可 该项目实现的功能如下: 获取手机相册,点击每个相 ...

  8. php如何获取图片地址,js如何直接获取网页中图片地址

    这次给大家带来js如何直接获取网页中图片地址,js直接获取网页中图片地址的注意事项有哪些,下面就是实战案例,一起来看一下. 第一种方法:js通过正则实现/** * 获取html代码中图片地址 * @p ...

  9. 删除目录下的特定命名的图片,获取特定名称图片的路径

    1.头文件 2.函数体--获取指定名称图片的路径 3.函数体-删除指定路径下指定名称的图片 转载于:https://www.cnblogs.com/1234abcdttttjy001/p/106553 ...

最新文章

  1. DP UVALive 6506 Padovan Sequence
  2. 启明云端分享| ESP32-S3点480*480分辨率的RGB 2.1寸旋钮屏刷新效果到底会怎么样呢
  3. 【转】Docker 运行时资源限制-内存memory、交换机分区Swap、CPU
  4. linux rm命令参数及用法详解---linux删除文件或目录命令
  5. 剑指offer-翻转单词顺序列
  6. webdriver 弹出框 java_如何使用Java处理Selenium WebDriver中的弹出窗口
  7. hget hmget redis api使用
  8. Dell笔记本耳机孔插进入没有反应问题
  9. 常微分和偏微分方程的区别是啥?
  10. 排列组合C几几怎么算
  11. 信息学奥赛一本通 1400:统计单词数 | 1954:【11NOIP普及组】统计单词数 | OpenJudge NOI 1.12 05 | 洛谷 P1308 [NOIP2011 普及组] 统计单词数
  12. 路由器工作原理及其主要部件详解
  13. T06-Linux创建用户和用户组
  14. 新月音标_又一个新月?
  15. 三,天猫精灵SDK驱动开发板LED
  16. 苹果电脑删除自带软件
  17. 7-4 银行排队问题之单窗口“夹塞”版 (30 分) C语言版
  18. 运算符、||运算符、?.可选链运算符、? ?空位合并运算符
  19. 隐患排查信息系统:实现安全隐患信息登记、评估、分类、处置、分析的流程化处理
  20. 视频允许播放禁止下载

热门文章

  1. yolo 识别 狗狗自行车
  2. elasticsearch python API
  3. 30. Leetcode 83. 删除排序链表中的重复元素 (链表-双指针)
  4. bash文件外传入参数
  5. torch_geometric 笔记:global_mean_pool
  6. Tableau可视化分析实战系列Tableau基础概念全解析 (一)-数据结构及字段
  7. Hadoop应用实战100讲(二)-Hadoop常用命令汇总
  8. 【Linux】18_日志管理rsyslog系统日志管理
  9. matlab读取.xyz文件及任意有间隔符数据
  10. Cracer渗透视频课程学习笔记——信息搜集