我的应用程序允许用户从图库或其他位置查看一些选定的图像。我通过以下方式请求图像的 Uri:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, PickerFragment.PICK_PHOTO);
Run Code Online (Sandbox Code Playgroud)
然后获取 onActivityResult (int requestCode, int resultCode, Intent data) 中的 Uri,并通过以下方式访问这些图像以将它们放入我的应用程序的视图中:
Uri imageUri= data.getData();
Run Code Online (Sandbox Code Playgroud)
当应用程序完成时,我通过以下方式将所有 Uri 作为字符串保存在数据库中:
String imageUriString = imageUri.toString();
saveToDB (imageUriString);
Run Code Online (Sandbox Code Playgroud)
但是,当我重新启动应用程序并通过以下方式从我的数据库恢复所有 Uri 时:
String imageUriString = restoreFromDB ();
imageUri = Uri.parse (imageUriString);
Run Code Online (Sandbox Code Playgroud)
在 Android 7.1 中,应用程序不再具有对这些 Uri 的权限。我通过以下方式访问图像:
ContentResolver cr = mContext.getContentResolver();
InputStream is = cr.openInputStream(imageUri);
BitmapFactory.decodeStream(is, null, o);
Run Code Online (Sandbox Code Playgroud)
但收到此异常:
java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.photos.contentprovider.impl.MediaContentProvider from ProcessRecord
Run Code Online (Sandbox Code Playgroud)
有没有一种优雅且合法的方式来保留该许可?我尝试阅读所有有关权限的文档,但感到非常困惑。
如果您能给我正确的学习方向,我将不胜感激。
PS 在 Uri 的许可持续期间将图像复制到应用程序内存中是一种选择,但我想避免这种情况。至少看看是否可能。
PPS 我拥有授予的所有权限,例如相机和外部存储写入和读取。