我注意到以下异常(Firebase Crashlytics):
Fatal Exception: java.lang.RuntimeException
Failure delivering result ResultInfo{who=null, request=1915322883, result=-1, data=Intent { dat=content:/... flg=0xc3 }} to activity {com..../com....Activity}: java.lang.SecurityException: No persistable permission grants found for UID 10571 and Uri content:/...
Fatal Exception: java.lang.SecurityException
No persistable permission grants found for UID 10434 and Uri content:/...
android.content.ContentResolver.takePersistableUriPermission (ContentResolver.java:2952)
Caused by android.os.RemoteException
Remote stack trace: at com.android.server.uri.UriGrantsManagerService.takePersistableUriPermission(UriGrantsManagerService.java:385) at android.app.IUriGrantsManager$Stub.onTransact(IUriGrantsManager.java:139) at android.os.Binder.execTransactInternal(Binder.java:1375) at android.os.Binder.execTransact(Binder.java:1311)
当应用程序调用
ContentResolver.takePersistableUriPermission
时:
val safResultLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult()
) { activityResult ->
coroutineScope.launch {
if (activityResult.resultCode == Activity.RESULT_OK) {
val treeUri = activityResult.data?.data
if (treeUri != null) {
context.contentResolver.takePersistableUriPermission(
treeUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
}
}
}
}
// select a folder
try {
safResultLauncher.launch(Intent.ACTION_OPEN_DOCUMENT_TREE)
} catch (e: Throwable) {
// ...
}
仅适用于三星设备(Android 13-14):
当您请求许可时,无法保证您会获得持久的许可。您的应用程序可以请求的数量有限(过去的限制是 128,但对于您的设备来说应该是 512)。此外,制造商或
DocumentsProvider
开发人员可能不会为用户所做的某些选择提供持久权限。
您需要优雅地处理
takePersistableUriPermission()
抛出异常的情况。