android - 使用 ACTION_GET_CONTENT 选择文件路径时获取文件路径的正确方法

标签 android android-intent android-file android-intent-chooser

我在我的应用中实现了一个文件选择器,如下所示:

Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Title"), FILE_PICK);

这是一个已知问题,您无法通过这种方式轻松获取实际文件位置,因为 Intent 会返回一些您无法真正用于创建 File 对象的奇怪 Uri。

我正在使用此方法获取实际文件路径:

/**
 * 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 the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

这对某些文件有效,对某些文件无效。现在我注意到当我从“下载”文件夹中选择一个文件时它不起作用。它会跳过上面的所有 if 语句并返回 null。

正确的方法是什么,这样可以更可靠地获取实际选择的文件路径?

最佳答案

I have a file picker implemented in my app like this

那不是“文件选择器”。它是一个内容选择器。

I need a way to be able to pick any file on Android device so that I can upload it to my backend.

嗯,这在很大程度上取决于您在该句子中的字面意思。

ACTION_GET_CONTENT 通常不是问题。但是,您得到的不一定是文件。但是,您仍然可以上传它们,前提是您用于上传的任何内容都允许您从 InputStream 上传。如果是这样,请在 ContentResolver 上使用 openInputStream(),传入 Uri,以获取要使用的 InputStream .

如果您真的需要它是实际文件,您可以直接在您的应用中实现文件选择器 UI。 There are libraries for this .但是,您将无法访问所有文件——特别是,您不能使用它从 removable storage 上传文件。在 Android 4.4+ 上。

关于android - 使用 ACTION_GET_CONTENT 选择文件路径时获取文件路径的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34858945/

相关文章:

android - 如何将 PdfDocument 对象保存到 Android 中的文件中?

java - Android 无法在 SD 卡上执行 mkdir()

java - Android InflaterInputStream 是否与流行的 ZLIB Windows 库相同?

从右到左使用 TextView 的 Android 设置

java - Fragment 白页上的 RecyclerView

email - 发送邮件 : Intent. EXTRA_EMAIL 不起作用

android - 从 Activity 向上导航回到 fragment

android - 无法在 onActivityResult 处 getIntExtra()

android - 如何从 Android 中的图库中删除文件?

android - 通过 Intent 而不是构造函数传递数据