根据Uri获取图片绝对路径,解决Android4.4以上版本Uri转换

  1. /** 
  2.  * 根据Uri获取图片绝对路径,解决Android4.4以上版本Uri转换 
  3.  * @param activity 
  4.  * @param imageUri 
  5.  * @author yaoxing 
  6.  * @date 2014-10-12 
  7.  */  
  8. @TargetApi(19)  
  9. public static String getImageAbsolutePath(Activity context, Uri imageUri) {  
  10.     if (context == null || imageUri == null)  
  11.         return null;  
  12.     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, imageUri)) {  
  13.         if (isExternalStorageDocument(imageUri)) {  
  14.             String docId = DocumentsContract.getDocumentId(imageUri);  
  15.             String[] split = docId.split(":");  
  16.             String type = split[0];  
  17.             if ("primary".equalsIgnoreCase(type)) {  
  18.                 return Environment.getExternalStorageDirectory() + "/" + split[1];  
  19.             }  
  20.         } else if (isDownloadsDocument(imageUri)) {  
  21.             String id = DocumentsContract.getDocumentId(imageUri);  
  22.             Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));  
  23.             return getDataColumn(context, contentUri, nullnull);  
  24.         } else if (isMediaDocument(imageUri)) {  
  25.             String docId = DocumentsContract.getDocumentId(imageUri);  
  26.             String[] split = docId.split(":");  
  27.             String type = split[0];  
  28.             Uri contentUri = null;  
  29.             if ("image".equals(type)) {  
  30.                 contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;  
  31.             } else if ("video".equals(type)) {  
  32.                 contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;  
  33.             } else if ("audio".equals(type)) {  
  34.                 contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;  
  35.             }  
  36.             String selection = MediaStore.Images.Media._ID + "=?";  
  37.             String[] selectionArgs = new String[] { split[1] };  
  38.             return getDataColumn(context, contentUri, selection, selectionArgs);  
  39.         }  
  40.     } // MediaStore (and general)  
  41.     else if ("content".equalsIgnoreCase(imageUri.getScheme())) {  
  42.         // Return the remote address  
  43.         if (isGooglePhotosUri(imageUri))  
  44.             return imageUri.getLastPathSegment();  
  45.         return getDataColumn(context, imageUri, nullnull);  
  46.     }  
  47.     // File  
  48.     else if ("file".equalsIgnoreCase(imageUri.getScheme())) {  
  49.         return imageUri.getPath();  
  50.     }  
  51.     return null;  
  52. }  
  53.   
  54. public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {  
  55.     Cursor cursor = null;  
  56.     String column = MediaStore.Images.Media.DATA;  
  57.     String[] projection = { column };  
  58.     try {  
  59.         cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);  
  60.         if (cursor != null && cursor.moveToFirst()) {  
  61.             int index = cursor.getColumnIndexOrThrow(column);  
  62.             return cursor.getString(index);  
  63.         }  
  64.     } finally {  
  65.         if (cursor != null)  
  66.             cursor.close();  
  67.     }  
  68.     return null;  
  69. }  
  70.   
  71. /** 
  72.  * @param uri The Uri to check. 
  73.  * @return Whether the Uri authority is ExternalStorageProvider. 
  74.  */  
  75. public static boolean isExternalStorageDocument(Uri uri) {  
  76.     return "com.android.externalstorage.documents".equals(uri.getAuthority());  
  77. }  
  78.   
  79. /** 
  80.  * @param uri The Uri to check. 
  81.  * @return Whether the Uri authority is DownloadsProvider. 
  82.  */  
  83. public static boolean isDownloadsDocument(Uri uri) {  
  84.     return "com.android.providers.downloads.documents".equals(uri.getAuthority());  
  85. }  
  86.   
  87. /** 
  88.  * @param uri The Uri to check. 
  89.  * @return Whether the Uri authority is MediaProvider. 
  90.  */  
  91. public static boolean isMediaDocument(Uri uri) {  
  92.     return "com.android.providers.media.documents".equals(uri.getAuthority());  
  93. }  
  94.   
  95. /** 
  96.  * @param uri The Uri to check. 
  97.  * @return Whether the Uri authority is Google Photos. 
  98.  */  
  99. public static boolean isGooglePhotosUri(Uri uri) {  
  100.     return "com.google.android.apps.photos.content".equals(uri.getAuthority());  
  101. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值