无法获取用于从 android 的 MI-Oreo(MI-A2) 设备的下载文件夹中选择任何文件的 URI

Can't get URI for selecting any file from download folder in MI-Oreo(MI-A2) device in android

我需要帮助来获取我们通过 Intent 从下载文件夹或任何其他文件夹中选择的内容的文件路径。 我附上了我一直在使用的 fileUtil class,它在 Nougat 版本之前运行良好。但是对于奥利奥,在某些特定设备中也是如此,我们在 return 中收到一个空路径。因此,如果有人遇到过此类错误并找到了解决方案,请在此处分享。

if(resultCode ==RESULT_OK)
{
    final Uri uri = data.getData();
    // Get the File path from the Uri
    String path = FileUtils.getPath(this, uri);
    // Alternatively, use FileUtils.getFile(Context, Uri)
    if (path != null && FileUtils.isLocal(path)) {
        //File file = new File(path);
        this.file = new File(path);
        profile_imagepath = path;
        filename = file.getName();
        txtselectedfile.setText(filename);
    }
}

您可以尝试使用ACTION_PICK for api level above 26 to get this issue fixed, instead of using ACTION_GET_CONTENT。 试试看,希望对你有帮助。

1) Open file intent.
       Intent intent = new Intent()
                    .setType("*/*")
                    .setAction(Intent.ACTION_GET_CONTENT);
            intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            intent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION);
            startActivityForResult(Intent.createChooser(intent, "Select a file"), REQUEST_CODE);

2)get download document path using below method.

  if (isDownloadsDocument(uri)) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    final String id;
                    Cursor cursor = null;
                    try {
                        cursor = context.getContentResolver().query(uri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null);
                        if (cursor != null && cursor.moveToFirst()) {
                            String fileName = cursor.getString(0);
                            String path = Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;
                            if (!TextUtils.isEmpty(path)) {
                                return path;
                            }
                        }
                    } finally {
                        if (cursor != null)
                            cursor.close();
                    }
                    id = DocumentsContract.getDocumentId(uri);
                    if (!TextUtils.isEmpty(id)) {
                        if (id.startsWith("raw:")) {
                            return id.replaceFirst("raw:", "");
                        }
                        String[] contentUriPrefixesToTry = new String[]{
                                "content://downloads/public_downloads",
                                "content://downloads/my_downloads"
                        };
                        for (String contentUriPrefix : contentUriPrefixesToTry) {
                            try {
                                final Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.valueOf(id));

                         /*   final Uri contentUri = ContentUris.withAppendedId(
                                    Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));*/

                                return getDataColumn(context, contentUri, null, null);
                            } catch (NumberFormatException e) {
                                //In Android 8 and Android P the id is not a number
                                return uri.getPath().replaceFirst("^/document/raw:", "").replaceFirst("^raw:", "");
                            }
                        }


                    }

                } else {
                    final String id = DocumentsContract.getDocumentId(uri);
                    final boolean isOreo = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
                    if (id.startsWith("raw:")) {
                        return id.replaceFirst("raw:", "");
                    }
                    try {
                        contentUri = ContentUris.withAppendedId(
                                Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }
                    if (contentUri != null) {
                        return getDataColumn(context, contentUri, null, null);
                    }
                }


            }