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?