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

转自:https://blog.csdn.net/huangyanan1989/article/details/17263203

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android webview cache 管理
  2. 实时获取Android 各版本电量的统一方法
  3. Android应用程序中模拟发送键盘触摸消息
  4. XAMARIN Android获取WIFIMAC地址的方法
  5. Android源码可行的下载步骤
  6. Android 网络请求框架 Retrofit2.0实践使
  7. Android中如何把bitmap存成BMP格式的图片
  8. Android培训班(32)
  9. Android-->原生API搭建Android Http服务
  10. Android中SparseArray源码实现