Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.SecurityException: Permission Denial: opening provider

I start image picker intent using:

final Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(intent, PICK_IMAGE);

and in onActivityResult() I get uris of all picked images and start Jobs which run in background and upload those images (https://github.com/yigit/android-priority-jobqueue). But if I press back button and exit out of activity then any job that was not started, can't access picked image when it runs and throws an exception:

java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.photos.contentprovider.MediaContentProvider from ProcessRecord{...} (pid=2407, uid=10117) that is not exported from uid 10123

The reason its happening is because permission is revoked once activity is finished. According to doc https://developer.android.com/guide/topics/providers/content-provider-basics.html:

These are permissions for a specific content URI that last until the activity that receives them is finished.

My question is, is there a workaround to this? Like getting permission at application level or something?

What are alternates to solve this problem? One quick solutions seems to be making a copy of all picked images and then uploading them but that seems like last resort.

like image 427
M-WaJeEh Avatar asked May 24 '16 09:05

M-WaJeEh


1 Answers

NOTE: Trying to get filename from uri is WRONG. Don't do it! Content providers can also share an arbitrary data that is not present in any file or file name could be misleading. E.g. content://downloads/some_secret_data could be pointing to a file that is not in downloads folder.

So best thing to do is to read/copy the data from content provider immediately and then use that data to do whatever you want. In my case I was uploading it.


Previous Wrong Answer (don't do it!):

This is what I did which is working fine for me. If someone has a better solution please do share. I had android.permission.READ_EXTERNAL_STORAGE so when user picked images I used concrete file paths instead of uris returned in onActivityResult(). To retrieve file paths I used this handy class https://stackoverflow.com/a/20559175/826606 and voila!

like image 61
M-WaJeEh Avatar answered Oct 14 '22 01:10

M-WaJeEh