需要android.permission.ACCESS_ALL_DOWNLOADS或grantUriPermission()

时间:2019-01-01 11:46:56

标签: android uri android-contentprovider android-permissions

当我要从所有下载文件夹中选择一个文件,然后使用内容提供程序从uri获取文件路径时,出现以下权限被拒绝的问题

Permission Denial: reading com.android.providers.downloads.DownloadProvider uri content://downloads/all_downloads/1092 from pid=31615, uid=10228 requires android.permission.ACCESS_ALL_DOWNLOADS, or grantUriPermission()

使用以下代码

String[] contentUriPrefixesToTry = new String[]{
                "content://downloads/public_downloads",
                "content://downloads/my_downloads",
                "content://downloads/all_downloads"
        };
        final String id = DocumentsContract.getDocumentId(uri);
        String path = null;
        for (String uriPath : contentUriPrefixesToTry) {
            final Uri contentUri = ContentUris.withAppendedId(
                    Uri.parse(uriPath), Long.valueOf(id));
            String lastPathSagment = uri.getLastPathSegment();
            InputStream inputStream = context.getContentResolver().openInputStream(uri);
            File file = new File(context.getCacheDir().getAbsolutePath() + "/" + lastPathSagment);
            writeFile(inputStream, file);
            path = file.getAbsolutePath();
             if (path != null) {
             return path;
             }
        }

2 个答案:

答案 0 :(得分:1)

我也遇到了上述问题,但是最后,我找到了解决方案。我使用ACTION_GET_CONTENT从外部存储中选择文件,但在Android 7及更高版本中不起作用,有人指导我使用ACTION_OPEN_DOCUMENT从Android 7及更高版本中选择文件。现在对我来说工作正常。

以下代码可在Android 7及更高版本中使用。

if (Build.VERSION.SDK_INT <19) {
        Intent pdfIntent = new Intent(Intent.ACTION_GET_CONTENT);
        pdfIntent.setType("application/pdf");
        startActivityForResult(pdfIntent, PICK_PDF);
    }else{
        Intent pdfIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        pdfIntent.setType("application/pdf");
        pdfIntent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(pdfIntent, PICK_PDF);
    }
if (isDownloadsDocument(uri)) {
   String fileName = getFilePath(context, uri);
   if (fileName != null) {
      return Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;
   }
   String id = DocumentsContract.getDocumentId(uri);
   if (id.startsWith("raw:")) {
      id = id.replaceFirst("raw:", "");
      File file = new File(id);
      if (file.exists())
         return id;
   }

   final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
   return getDataColumn(context, contentUri, null, null);
}

public static String getFilePath(Context context, Uri uri) {

    Cursor cursor = null;
    final String[] projection = {
            MediaStore.MediaColumns.DISPLAY_NAME
    };

    try {
        cursor = context.getContentResolver().query(uri, projection, null, null,
                null);
        if (cursor != null && cursor.moveToFirst()) {
            final int index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
            return cursor.getString(index);
        }
    } catch(Exception e) {
        e.printStackTrace();
    }finally
     {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

答案 1 :(得分:-1)

您的代码正在“尝试”访问该目录以查看其是否存在,甚至打开输入流以获取假定的文件。如果该目录不存在,Android将对您不满意,并告诉您不允许您访问该目录。 不需要请求权限,因为该目录不存在。