2

Can't access file with intent by passing its uri / UID 10153 does not have permission to content

I have an mailbox type application with attachments attached to messages. When you download an attachment, you can open this attachment via intent with its uri passed. The uri looks like this: "content://com.android.providers.downloads.documents/document/13".

I save the downloaded attachments to shared preferences by converting the object containing the path and attachment ID to JSON with GSON and storing it as a string, so I can fetch the list when accessing the application again, so I don't lose the links to files, so the user would need to always download them again.

I compare the IDs and assign onClickListener to the attachment that has already been saved. On this onClickListener, I put an intent for opening the file, which works only if I don't restart my application.

attachmentFileName?.setOnClickListener {
                        Log.i("URI", attachmentDownloaded.path)
                        val intent = Intent(Intent.ACTION_VIEW)
                        intent.setDataAndType(attachmentDownloaded.path.toUri(), attachment.mimeType)
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                        startActivity(activity, intent, null)
                    }

But if I try to open the attachment file after I restart the application, it throws an SecurityException.

java.lang.SecurityException: UID 10153 does not have permission to content://com.android.providers.downloads.documents/document/13 [user 0]; you could obtain access using ACTION_OPEN_DOCUMENT or related APIs

If I try using ACTION_OPEN_DOCUMENT like the error says, I get the same exception.

Anyone knows why this is happening?

Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
  • "When you download an attachment, you can open this attachment via intent with its uri passed" -- how are you downloading the attachment? If the answer is "via `DownloadManager`", switch to downloading the attachment yourself, [using OkHttp](https://stackoverflow.com/a/29012988/115145) or your favorite HTTP client API. – CommonsWare Aug 05 '21 at 13:27
  • As @CommonsWare suggested, you could use your own downloader using a custom client or libary. If you want to use the `DownloadManager` then you will have to write a custom `ContentProvider` to get access to the `Downloads` directory. That's what I am guessing – gtxtreme Aug 05 '21 at 15:59
  • @gtxtreme, No. DownloadManager will give you an uri for the downloaded file. You can use that uri to serve the file. – blackapps Aug 05 '21 at 19:18

1 Answers1

1

Maybe it's happening because you are using android 10 or 11. You can't access the URI in android 11 like that. If you have picked any images from the system gallery, then it's fine. But if you are saving the URI path to string and then accessing it after restarting your app, then you can't do it in android 11.

I also faced this issue before. There's one simple solution to it. If you want to access the file for later use in your app, the entry copying it from the original location to your app's cache or files directory.

You'll find plenty of documents on it. Just save your file into the files directory when you first get the URI of your files.

So, The second time you open your app, you're not going to use the original URI of that file, but the saved one's path from your app's internal app directory or cache directory, wherever you saved it.

Dev4Life
  • 2,328
  • 8
  • 22