Android 10:能够直接上传 Uri,但稍后重新尝试时失败并出现 "Permission denial" 异常
Android 10: Able to upload a Uri directly but fails with "Permission denial" exception when re-attempted later
使用 FilePicker,我可以让用户选择要上传的文件,将其传递给 IntentService,如果设备有网络,则立即通过该 intentService 上传。
但是如果没有网络,我需要保存 Uri 并在设备获得网络后尝试上传。这次重新尝试失败了。当我在重新尝试期间尝试启动服务时,它抛出 "Permission denial" 异常。请让我知道可能出了什么问题。感谢您的帮助。
FILEPICKER
-----------
openFilePickDialog = new Intent();
openFilePickDialog.setType("*/*");
openFilePickDialog.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(openFilePickDialog, "Select File"), PickFileId);
During re-attempt of upload
----------------------------
Intent iUploadService = new Intent(context, UploadService.class);
String uriString = pendingUpload.uriString;
iUploadService.setData(Uri.parse(uriString));
iUploadService.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
iUploadService.putExtra(UploadService.ACTION, UploadService.ACTION_UPLOAD);
context.startService(iUploadService);
Stacktrace
-----------
java.lang.SecurityException: UID 10140 does not have permission to content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2Fcom.aaaa.bbbb%2Ffiles%2FFolder%2FAttachments%2F1587129056397_IMG-20200414-WA0004.jpg [user 0]; you could obtain access using ACTION_OPEN_DOCUMENT or related APIs
at android.os.Parcel.createException(Parcel.java:2071)
at android.os.Parcel.readException(Parcel.java:2039)
at android.os.Parcel.readException(Parcel.java:1987)
at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:5166)
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1601)
at android.app.ContextImpl.startService(ContextImpl.java:1571)
at android.content.ContextWrapper.startService(ContextWrapper.java:669)
不要使用 ACTION_GET_CONTENT 因为您已经看到读取权限不会持续很长时间。
改为使用 ACTION_OPEN_DOCUMENT 并在 onActivityResult 中获取持久化 uri 权限。
使用 FilePicker,我可以让用户选择要上传的文件,将其传递给 IntentService,如果设备有网络,则立即通过该 intentService 上传。 但是如果没有网络,我需要保存 Uri 并在设备获得网络后尝试上传。这次重新尝试失败了。当我在重新尝试期间尝试启动服务时,它抛出 "Permission denial" 异常。请让我知道可能出了什么问题。感谢您的帮助。
FILEPICKER
-----------
openFilePickDialog = new Intent();
openFilePickDialog.setType("*/*");
openFilePickDialog.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(openFilePickDialog, "Select File"), PickFileId);
During re-attempt of upload
----------------------------
Intent iUploadService = new Intent(context, UploadService.class);
String uriString = pendingUpload.uriString;
iUploadService.setData(Uri.parse(uriString));
iUploadService.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
iUploadService.putExtra(UploadService.ACTION, UploadService.ACTION_UPLOAD);
context.startService(iUploadService);
Stacktrace
-----------
java.lang.SecurityException: UID 10140 does not have permission to content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2Fcom.aaaa.bbbb%2Ffiles%2FFolder%2FAttachments%2F1587129056397_IMG-20200414-WA0004.jpg [user 0]; you could obtain access using ACTION_OPEN_DOCUMENT or related APIs
at android.os.Parcel.createException(Parcel.java:2071)
at android.os.Parcel.readException(Parcel.java:2039)
at android.os.Parcel.readException(Parcel.java:1987)
at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:5166)
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1601)
at android.app.ContextImpl.startService(ContextImpl.java:1571)
at android.content.ContextWrapper.startService(ContextWrapper.java:669)
不要使用 ACTION_GET_CONTENT 因为您已经看到读取权限不会持续很长时间。
改为使用 ACTION_OPEN_DOCUMENT 并在 onActivityResult 中获取持久化 uri 权限。