【问题标题】:Android 7 nougat, how to get the file path from uri of incoming intent?Android 7 nougat,如何从传入意图的uri中获取文件路径?
【发布时间】:2017-08-27 23:34:14
【问题描述】:

FileProvider:- Setting up file sharing

我知道在 android nougat 中更改文件策略。 文件共享给其他应用程序的所需应用程序通过 FileProvider 生成 uri。 uri 格式为content://com.example.myapp.fileprovider/myimages/default_image.jpg

我想知道如何从 FileProvider.getUriForFile() 生成的 uri 中获取文件路径。 因为我的应用程序需要知道物理文件路径才能保存、加载、读取信息等。 有没有可能

[简而言之]

  1. 我的应用收到了其他应用在 andorid 7 nougat 上的意图 uri。
  2. uri 格式为content://com.example.myapp.fileprovider/myimages/default_image.jpg
  3. 可能是FileProvider.getUriForFile生成的。
  4. 我想知道从 uri 获取文件路径的方法。
  5. 我可以从getContentResolver().openFileDescriptor() 获取 mime 类型、显示名称、文件大小和读取二进制文件。 但我想知道文件路径。

【问题讨论】:

  • 如果可以从流中二进制读取,则可以从流中读取。用它。不需要文件路径。只有文件提供者应该能够告诉你文件路径,但它不会。重新思考。重新设计。您不需要文件路径。
  • "我的应用需要知道物理文件路径才能保存、加载、读取信息等。" -- 然后使用ContentResolveropenInputStream() 制作您自己的内容副本。您不仅无法确定路径,而且即使可以,您也很可能无论如何都没有文件系统访问该文件的权限。
  • 您找到解决方案了吗?

标签: android android-intent filepath android-7.0-nougat android-fileprovider


【解决方案1】:

您无法获得 Uri 的“文件路径”,原因很简单,即不需要 Uri 指向文件。使用ContentResolver 和openInputStream() 等方法访问Uri 表示的内容。

要使用内容 URI 与其他应用共享文件,您的应用必须 生成内容 URI。要生成内容 URI,请创建一个新的 文件的文件,然后将文件传递给 getUriForFile()。你可以发送 getUriForFile() 返回到另一个应用程序的内容 URI 意图。接收到内容 URI 的客户端应用程序可以打开文件 并通过调用 ContentResolver.openFileDescriptor 访问其内容 获取 ParcelFileDescriptor。 来源:Android developers

【讨论】:

  • To share a file with another app using a content URI, your app has to 实现一个 FileProvider。
  • 如果我使用本机库播放视频文件怎么办(C++ 对 Content URI 一无所知,它需要绝对路径)?我从某个资源管理器应用程序中选择了一个视频文件,并尝试在我的视频播放器应用程序中打开它
【解决方案2】:

我在我们的应用中也有类似的要求,但之前很困惑。我是这样解决的。

在 Android N 中,只有向 3rd 方应用程序公开的文件 uri 被更改。 (不是我们以前使用它的方式)。

在我们的应用程序中,我们将 uri 发送到相机应用程序,在该位置,我们期望相机应用程序存储捕获的图像。

  1. 对于 android N,我们生成新的基于 Content:// uri 的 url 指向 文件。
  2. 我们为相同的文件生成通常的基于 File api 的路径(使用旧方法)。

现在我们有 2 个不同的 uri 用于同一个文件。 #1 与相机应用程序共享。如果相机意图成功,我们可以从 #2 访问图像。

希望这会有所帮助。

【讨论】:

    【解决方案3】:
    // Use Below Method Working fine for Android N.
    private static String getFilePathForN(Uri uri, Context context) {
        Uri returnUri = uri;
        Cursor returnCursor = context.getContentResolver().query(returnUri, null, null, null, null);
        /*
         * Get the column indexes of the data in the Cursor,
         *     * move to the first row in the Cursor, get the data,
         *     * and display it.
         * */
        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
        returnCursor.moveToFirst();
        String name = (returnCursor.getString(nameIndex));
        String size = (Long.toString(returnCursor.getLong(sizeIndex)));
        File file = new File(context.getFilesDir(), name);
        try {
            InputStream inputStream = context.getContentResolver().openInputStream(uri);
            FileOutputStream outputStream = new FileOutputStream(file);
            int read = 0;
            int maxBufferSize = 1 * 1024 * 1024;
            int bytesAvailable = inputStream.available();
    
            //int bufferSize = 1024;
            int bufferSize = Math.min(bytesAvailable, maxBufferSize);
    
            final byte[] buffers = new byte[bufferSize];
            while ((read = inputStream.read(buffers)) != -1) {
                outputStream.write(buffers, 0, read);
            }
            Log.e("File Size", "Size " + file.length());
            inputStream.close();
            outputStream.close();
            Log.e("File Path", "Path " + file.getPath());
    
        } catch (Exception e) {
            Log.e("Exception", e.getMessage());
        }
        return file.getPath();
    }
    

    【讨论】:

    • 以上方法在 Android N 中工作。您将获得文件路径 ContentResolver() 并在 android Manifext 文件中使用文件提供程序以及读取和写入外部存储权限使用。上面的方法只是在 OnActivityResult 中调用 Data uri 获取并简单地调用 String strPath=getFilePathForN(uri,this);
    • 请始终将您的答案放在上下文中,而不仅仅是粘贴代码。有关详细信息,请参阅here
    猜你喜欢
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2011-03-01
    • 2019-08-17
    • 1970-01-01
    • 2020-03-26
    相关资源
    最近更新 更多