0

I've been trying to find the answer without success for this exception that is being thrown when I pick a file from the downloads folder on API 28, which ends up being cached as a fallback and retrieved the path nevertheless.

TL;DR: What do I need to fix this exception?

java.lang.SecurityException: Permission Denial: reading 
com.android.providers.downloads.DownloadProvider uri 
content://downloads/all_downloads/40 from pid=13467, uid=10091 requires 
android.permission.ACCESS_ALL_DOWNLOADS, or grantUriPermission()

Things I've already tried so far:

Add this to the manifest file:

   <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_ALL_DOWNLOADS"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name = "android.permission.ACCESS_DOWNLOAD_MANAGER"/>

and requesting Manifest.permission.WRITE_EXTERNAL_STORAGE in runtime.

Miguel Ruivo
  • 16,035
  • 7
  • 57
  • 87
  • How exactly did you get that `Uri`? And, by "ends up being cached", do you mean that you are writing that `Uri` to disk and trying to use it again later? – CommonsWare Jul 06 '19 at 00:02
  • Yes, the path resolution tries to load it from a few prefixes and I get this exception in some of them. Since it ends up failing, the path resolution goes on and I end up loading it and caching from its `String externalFile = context.getCacheDir().getAbsolutePath() + "/" + FileUtils.getFileName(uri, context);` – Miguel Ruivo Jul 06 '19 at 00:04
  • OK... so how exactly did you get that `Uri`? – CommonsWare Jul 06 '19 at 00:06
  • I mean: `context.getContentResolver().openInputStream(uri);` and then I write it to the path previous assigned. But I'm trying to retrieve the absolute path of a file that is in the downloads folder (using the native file explorer) but somehow it seems impossible for some kind of files (in the downloads folder) and I have to use the `contentResolver`, make a copy of it, and then use the copy's path. – Miguel Ruivo Jul 06 '19 at 00:09
  • I am facing the exact same problem. Did you ever come up with a solution? – Hyndrix Nov 07 '19 at 07:18

1 Answers1

0

Lets assume you want to access downloads folder to pick up an image file

Intent intent = new Intent();
intent.setType("image/*");
if (Build.VERSION.SDK_INT <19)
    intent.setAction(Intent.ACTION_GET_CONTENT);
else
    intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
startActivityForResult(Intent.createChooser(intent, "Select Image"), requestSelectImage);
robsiemb
  • 6,157
  • 7
  • 32
  • 46