java 用户拒绝对代码授予权限,java.lang.SecurityException:权限拒绝:打开提供程序...

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.

解决方案

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值