【问题标题】:Convert content:// URI to actual path in Android 4.4在 Android 4.4 中将 content:// URI 转换为实际路径
【发布时间】:2013-11-27 21:56:06
【问题描述】:

我尝试了一个可以正常工作的解决方案(见下文),但在 Android 4.4 中,对startActivityForResult() 的调用会显示一个名为“打开自”的活动,其中还包含“最近”、“图像”、“下载”作为几个可供选择的应用程序。当我选择“图像”并尝试解析返回的内容 URI(使用下面的代码)时,对 cursor.getString() 的调用返回 null。如果我使用 Gallery 应用程序选择完全相同的文件,cursor.getString() 返回一个文件路径。我只在 API 级别 16 和 19 中对此进行了测试。在 16 中一切正常。就 19 而言,我必须选择 Gallery 或其他应用程序,否则它不起作用。

private String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try { 
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);

        return path;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

【问题讨论】:

  • 您找到解决方案了吗?

标签: android filepath android-contentresolver


【解决方案1】:

我知道它本身并不能回答问题,但正如 @CommonsWare 所难过的那样,SAF 并不打算使用这种方式。

也许一个选项是在应用程序的外部文件目录上创建文件的副本,使用它然后删除它。

public File createFileCopy(Context context, DocumentFile file) {

        if (file == null || !file.exists() || file.getName() == null) {
            throw new IllegalArgumentException("The file must no be null, and must exist, and must have a name.");
        }

        File fileCopy = new File(context.getExternalFilesDir(null).getAbsolutePath(), file.getName());

        try {

            android.os.FileUtils.copy(openFileInputStream(file), new FileOutputStream(fileCopy));

            return fileCopy;

        } catch (Exception e) {
          // do whateveer you want with this exceeption
            e.printStackTrace();
        }
        return null;
    }

【讨论】:

    【解决方案2】:

    从 Uri 获取文件路径:-我创建了一个 Util 类,它将获取 Storage Access Framework Documents 的路径,以及 MediaStore 和其他 基于文件的 ContentProviders 的 _data 字段。

    ConvertUriToFilePath :-

    import android.content.ContentUris;
    import android.content.Context;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Build;
    import android.os.Environment;
    import android.provider.DocumentsContract;
    import android.provider.MediaStore;
    import android.support.annotation.RequiresApi;
    
    public class ConvertUriToFilePath {
        /**
         * Get a file path from a Uri. This will get the the path for Storage Access
         * Framework Documents, as well as the _data field for the MediaStore and
         * other file-based ContentProviders.
         *
         * @param context The context.
         * @param uri     The Uri to query.
         */
        @RequiresApi(api = Build.VERSION_CODES.KITKAT)
        public static String getPathFromURI(final Context context, final Uri uri) {
    
            final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if (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
            }
            // DownloadsProvider
            else 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);
            }
            // MediaProvider
            else 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())) {
    
            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);
    
    
            //  return getDataColumn(context, uri, null, null);
        }
        // File
        else 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 column_index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(column_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());
    }
    

    }

    示例代码:

    // Just call this function of ConvertUriToFilePath class and it will return full path of file URI.
    
     String actualFilepath= ConvertUriToFilePath.getPathFromURI(activity,tempUri);
    

    【讨论】:

    • 我的内容为空://com.android.chrome.FileProvider/images/screenshot/15078254878111317987290.jpg
    • 发布链接中提供的实际代码的好处是链接断开时不会丢失。不幸的是,您的回答只提供了一个充满广告的页面。
    【解决方案3】:

    感谢@FireBear,我现在修改了答案,将获得媒体文件的路径

    String filePath=saveBitmap(activity,getBitmapFromUri(imageUri),"tmpFile").getPath();

    private Bitmap getBitmapFromUri(Context context, Uri uri) throws IOException {
            ParcelFileDescriptor parcelFileDescriptor =
                    context.getContentResolver().openFileDescriptor(uri, "r");
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
            parcelFileDescriptor.close();
            return image;
        }
    
        private File saveBitmap(Context context, Bitmap bitmap, String name) {
            File filesDir = context.getFilesDir();
            File imageFile = new File(filesDir, name + ".jpg");
            OutputStream os;
            try {
                os = new FileOutputStream(imageFile);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
                os.flush();
                os.close();
            } catch (Exception e) {
                //Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
            }
            return imageFile;
        }
    

    【讨论】:

      【解决方案4】:

      这将从 MediaProvider、DownloadsProvider 和 ExternalStorageProvider 获取文件路径,同时回退到您提到的非官方 ContentProvider 方法。

         /**
       * Get a file path from a Uri. This will get the the path for Storage Access
       * Framework Documents, as well as the _data field for the MediaStore and
       * other file-based ContentProviders.
       *
       * @param context The context.
       * @param uri The Uri to query.
       * @author paulburke
       */
      public static String getPath(final Context context, final Uri uri) {
      
          final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
      
          // DocumentProvider
          if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
              // ExternalStorageProvider
              if (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
              }
              // DownloadsProvider
              else 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);
              }
              // MediaProvider
              else 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 getDataColumn(context, uri, null, null);
          }
          // File
          else 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 column_index = cursor.getColumnIndexOrThrow(column);
                  return cursor.getString(column_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());
      }
      

      sourceaFileChooser

      【讨论】:

      • 这很好用。要添加的一件事是,为了让我的上传代码(通过改造)工作,我必须在返回的字符串的开头附加“file://”。
      • 适用于“content://downloads/all_downloads/47”等 URI,但不适用于“content://downloads/my_downloads/47”(由 Chrome 在您打开文件时生成刚刚下载的)
      • 此实用程序并不完美,会在 Galaxy S7、Andorid N API24 上导致 java.lang.RuntimeException。错误是相机拍照时“_data”列不存在。
      • 对 Oreo 的 Downloads 文件夹中的文件抛出异常
      • @KishanVaghela 此代码不适用于来自 GoogleDrive 和 Uri 类型 =>"content://com.google.android.apps.docs.storage/document/acc%3D3%3Bdoc%3D1259 的文件"
      【解决方案5】:

      a Google API中介绍。你可以试试这个:

      private Bitmap getBitmapFromUri(Uri uri) throws IOException {
          ParcelFileDescriptor parcelFileDescriptor =
                  getContentResolver().openFileDescriptor(uri, "r");
          FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
          Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
          parcelFileDescriptor.close();
          return image;
      }
      

      【讨论】:

      • 我什么时候需要文件呢?
      • 开始出现 java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider 异常
      • 最佳解决方案!!
      【解决方案6】:

      如果你真的需要一个文件路径。首先,使用 ContentResolver 获取数据。然后,您可以将数据保存到临时文件并使用该路径。

      (我必须在函数参数中使用带有 File 对象的库。)

      【讨论】:

        【解决方案7】:

        我也遇到过这个问题,但就我而言,我想做的是为 Gallery 指定一个具体的 Uri,以便以后可以使用裁剪。看起来在 KitKat 的新文档浏览器中,我们不能再这样做了,除非您在导航抽屉中选择图库,并且如您所说,直接从那里打开图像或文件。

        在 Uri 的情况下,您仍然可以在从文档浏览器打开时检索路径。

            Intent dataIntent= new Intent(Intent.ACTION_GET_CONTENT);
            dataIntent.setType("image/*"); //Or whatever type you need
        

        然后在onActivityResult中:

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == ACTIVITY_SELECT_IMAGE && resultCode == RESULT_OK) {
                myUri = data.getData();
                String path = myUri.getPath();
                openPath(myUri);
        
            }
        }
        

        如果您需要使用该路径打开文件,您只需使用内容解析器:

        public void openPath(Uri uri){
            InputStream is = null;
            try {
                is = getContentResolver().openInputStream(uri);
                //Convert your stream to data here
                is.close();
        
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        

        【讨论】:

        • 这是我想要的解决方案。
        【解决方案8】:

        在 Android 4.4 中将 content:// URI 转换为实际路径

        在任何 Android 版本上都没有可靠的方法来执行此操作。 content:// Uri 不必代表文件系统上的文件,更不用说您可以访问的文件了。

        Android 4.4 对提供存储框架的更改只是增加了您遇到content://Uri 值的频率。

        如果您获得content:// Uri,请使用ContentResolveropenInputStream()openOutputStream() 等方法使用它。

        【讨论】:

        • 当使用 Intent.ACTION_GET_CONTENT 专门选择视频时,假设 MediaStore 提供程序将保存与返回的内容 URI 相关的信息是否不正确?
        • @TomReznik:不要求ACTION_GET_CONTENT 返回已被MediaStore 索引的Uri
        • @CommonsWare 感谢您的回答,每个人似乎都在做海报所做的事情,尽管从来没有任何保证它会一直有效。我现在的问题是,如果我们需要一个 File 而不是 InputStream,这是否意味着我们必须将 InputStream 转换为 File?
        • @a_secret:首先,我会尝试为您要解决的任何问题找到一些其他解决方案,一个不涉及File 的解决方案(参见去年关于此主题的this rant of mine )。否则,是的,您需要将InputStream 的内容流式传输到您自己的本地文件中。
        • @CommonsWare 感谢您的建议;我确实已经恢复使用 InputStream 了!问题更多的是我想确保我的理解是正确的(作为一种求知欲)。谢谢!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 2015-02-20
        • 2018-12-04
        相关资源
        最近更新 更多