转至 http://blog.csdn.net/tempersitu/article/details/20557383

最近在做一个从图库选择图片或拍照,然后裁剪的功能.本来是没问题的,一直在用

  1. Intent intent=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

的方式来做,是调用系统图库来做,但是发现如果有图片是同步到google相册的话,图库里面能看到一个auto backup的目录,点进去选图片的话是无法获取到图片的路径的.因为那些图片根本就不存在于手机上.然后看到无论是百度贴吧,Instagram,或者还有些会选取图片做修改的app,都是用一个很漂亮的图片选择器(4.4以上,4.3的还是用系统旧的图库).

而这个图片选择器可以屏蔽掉那个auto backup的目录.所以就开始打算用这个图片选择器来选图片了.
这个方法就是

  1. Intent intent=new Intent(Intent.ACTION_GET_CONTENT);//ACTION_OPEN_DOCUMENT
  2. intent.addCategory(Intent.CATEGORY_OPENABLE);
  3. intent.setType("image/jpeg");
  4. if(android.os.Build.VERSION.SDK_INT>=android.os.Build.VERSION_CODES.KITKAT){
  5. startActivityForResult(intent, SELECT_PIC_KITKAT);
  6. }else{
  7. startActivityForResult(intent, SELECT_PIC);
  8. }

为什么要分开不同版本呢?其实在4.3或以下可以直接用ACTION_GET_CONTENT的,在4.4或以上,官方建议用ACTION_OPEN_DOCUMENT,但其实都不算太大区别,区别是他们返回的Uri,那个才叫大区别.这就是困扰了我一整天的问题所在了.

4.3或以下,选了图片之后,根据Uri来做处理,很多帖子都有了,我就不详细说了.主要是4.4,如果使用上面pick的原生方法来选图,返回的uri还是正常的,但如果用ACTION_GET_CONTENT的方法,返回的uri跟4.3是完全不一样的,4.3返回的是带文件路径的,而4.4返回的却是content://com.android.providers.media.documents/document/image:3951这样的,没有路径,只有图片编号的uri.这就导致接下来无法根据图片路径来裁剪的步骤了.

还好找了很多方法,包括加权限啊什么的,中间还试过用一些方法,自己的app没崩溃,倒是让系统图库崩溃了,引发了java.lang.SecurityException.

  1. Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{437b5d88 9494:com.google.android.gallery3d/u0a20} (pid=9494, uid=10020) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS

看来4.4的系统还是有些bug.重点来了,4.4得到的uri,需要以下方法来获取文件的路径

  1. public static String getPath(final Context context, final Uri uri) {
  2. final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  3. // DocumentProvider
  4. if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  5. // ExternalStorageProvider
  6. if (isExternalStorageDocument(uri)) {
  7. final String docId = DocumentsContract.getDocumentId(uri);
  8. final String[] split = docId.split(":");
  9. final String type = split[0];
  10. if ("primary".equalsIgnoreCase(type)) {
  11. return Environment.getExternalStorageDirectory() + "/" + split[1];
  12. }
  13. // TODO handle non-primary volumes
  14. }
  15. // DownloadsProvider
  16. else if (isDownloadsDocument(uri)) {
  17. final String id = DocumentsContract.getDocumentId(uri);
  18. final Uri contentUri = ContentUris.withAppendedId(
  19. Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
  20. return getDataColumn(context, contentUri, null, null);
  21. }
  22. // MediaProvider
  23. else if (isMediaDocument(uri)) {
  24. final String docId = DocumentsContract.getDocumentId(uri);
  25. final String[] split = docId.split(":");
  26. final String type = split[0];
  27. Uri contentUri = null;
  28. if ("image".equals(type)) {
  29. contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  30. } else if ("video".equals(type)) {
  31. contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  32. } else if ("audio".equals(type)) {
  33. contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  34. }
  35. final String selection = "_id=?";
  36. final String[] selectionArgs = new String[] {
  37. split[1]
  38. };
  39. return getDataColumn(context, contentUri, selection, selectionArgs);
  40. }
  41. }
  42. // MediaStore (and general)
  43. else if ("content".equalsIgnoreCase(uri.getScheme())) {
  44. // Return the remote address
  45. if (isGooglePhotosUri(uri))
  46. return uri.getLastPathSegment();
  47. return getDataColumn(context, uri, null, null);
  48. }
  49. // File
  50. else if ("file".equalsIgnoreCase(uri.getScheme())) {
  51. return uri.getPath();
  52. }
  53. return null;
  54. }
  55. /**
  56. * Get the value of the data column for this Uri. This is useful for
  57. * MediaStore Uris, and other file-based ContentProviders.
  58. *
  59. * @param context The context.
  60. * @param uri The Uri to query.
  61. * @param selection (Optional) Filter used in the query.
  62. * @param selectionArgs (Optional) Selection arguments used in the query.
  63. * @return The value of the _data column, which is typically a file path.
  64. */
  65. public static String getDataColumn(Context context, Uri uri, String selection,
  66. String[] selectionArgs) {
  67. Cursor cursor = null;
  68. final String column = "_data";
  69. final String[] projection = {
  70. column
  71. };
  72. try {
  73. cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
  74. null);
  75. if (cursor != null && cursor.moveToFirst()) {
  76. final int index = cursor.getColumnIndexOrThrow(column);
  77. return cursor.getString(index);
  78. }
  79. } finally {
  80. if (cursor != null)
  81. cursor.close();
  82. }
  83. return null;
  84. }
  85. /**
  86. * @param uri The Uri to check.
  87. * @return Whether the Uri authority is ExternalStorageProvider.
  88. */
  89. public static boolean isExternalStorageDocument(Uri uri) {
  90. return "com.android.externalstorage.documents".equals(uri.getAuthority());
  91. }
  92. /**
  93. * @param uri The Uri to check.
  94. * @return Whether the Uri authority is DownloadsProvider.
  95. */
  96. public static boolean isDownloadsDocument(Uri uri) {
  97. return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  98. }
  99. /**
  100. * @param uri The Uri to check.
  101. * @return Whether the Uri authority is MediaProvider.
  102. */
  103. public static boolean isMediaDocument(Uri uri) {
  104. return "com.android.providers.media.documents".equals(uri.getAuthority());
  105. }
  106. /**
  107. * @param uri The Uri to check.
  108. * @return Whether the Uri authority is Google Photos.
  109. */
  110. public static boolean isGooglePhotosUri(Uri uri) {
  111. return "com.google.android.apps.photos.content".equals(uri.getAuthority());
  112. }

这样,就可以在4.4上用漂亮的图片选择器,选到我们想要的文件,又不会出问题了.

昨天发现了个bug,如果在4.4上面不用"图片"来选,用"图库"来选,就会无法读取到图片路径,所以只需要加个判断,如果是用旧方式来选,就用旧方式来读,就是如果
DocumentsContract.isDocumentUri(context, uri) 返回false的话,就用旧的方式

  1. public static String selectImage(Context context,Intent data){
  2. Uri selectedImage = data.getData();
  3. //      Log.e(TAG, selectedImage.toString());
  4. if(selectedImage!=null){
  5. String uriStr=selectedImage.toString();
  6. String path=uriStr.substring(10,uriStr.length());
  7. if(path.startsWith("com.sec.android.gallery3d")){
  8. Log.e(TAG, "It's auto backup pic path:"+selectedImage.toString());
  9. return null;
  10. }
  11. }
  12. String[] filePathColumn = { MediaStore.Images.Media.DATA };
  13. Cursor cursor = context.getContentResolver().query(selectedImage,filePathColumn, null, null, null);
  14. cursor.moveToFirst();
  15. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  16. String picturePath = cursor.getString(columnIndex);
  17. cursor.close();
  18. return picturePath;
  19. }

这样就OK的了

最新文章

  1. jQuery如何改变css伪元素样式
  2. POJ2965
  3. SQL Server 数据库的维护(一)__存储过程(procedure)
  4. http://www.cnblogs.com/TankXiao/p/4018219.html
  5. fltk_hello world
  6. [OC Foundation框架 - 22] 集合的内存管理
  7. linux c/c++ GDB教程详解(转)
  8. jQuery json数据处理
  9. Javabean的理解
  10. JavaFX2: 鼠标拖动选择和Ctrl+Shift连续区间选择的ListView
  11. Robots协议
  12. ASP.NET 导出EXCEL文件处理多对应排列的
  13. 35 个 jQuery 小技巧
  14. linux上 java 使用 javasqlite
  15. 关于微信JS-SDK 分享接口的两个报错记录
  16. Python——plot可视化数据,作业8(python programming)
  17. git命令行获取某分支代码
  18. Windows环境下实现Consul服务注册和服务发现
  19. [转]有关Apache alias的一点问题
  20. ios中两个view动画切换

热门文章

  1. 【Socket】linux组播技术
  2. 关于emoji表情
  3. 解决opencv和mfc同时使用导致memory leak
  4. 【Java】PreparedStatement VS Statement
  5. Linux考前突击
  6. 基于jQuery图片元素网格布局插件
  7. Mysql注入绕过姿势
  8. 微信web开发者工具同时打开两个小程序项目
  9. GODOT 3.0 开发快照版本 ALPHA1 释出
  10. Redis (1) —— 安装