如何在 android API 19 (KitKat) 中保留权限?
2022-09-03 00:24:22
在我的应用程序中,我将图像的路径存储在我的SQlite数据库中以供进一步使用。我得到的路径是
content://com.android.providers.media.documents/document/image%3A71964
当我从数据库中检索此路径并尝试从Android抛出的路径中检索图像时
java.lang.SecurityException: Permission Denial: opening provider
com.android.providers.media.MediaDocumentsProvider
from ProcessRecord{42c84ec8 23911:com.gots.gb/u0a248} (pid=23911, uid=10248)
requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
根据 https://developer.android.com/guide/topics/providers/document-provider.html#permissions 我需要通过添加以下代码来持久化权限
final int takeFlags = intent.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Check for the freshest data.
getContentResolver().takePersistableUriPermission(uri, takeFlags);
当我将此代码添加到我的ImageAdapter类中时,该类扩展了BaseAdapter android throws
08-21 02:14:38.530: W/System.err(24452): java.lang.SecurityException:
No permission grant found for UID 10248 and Uri
content://com.android.providers.media.documents/document/image:71964
这是我的图像适配器代码的相关部分
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView ;
if (convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else{
imageView = (ImageView)convertView ;
}
InputStream is = null;
Bitmap bitmap = null ;
try {
Log.d(TAG,String.valueOf(list.get(position)));
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
final int takeFlags = intent.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Check for the freshest data.
if (Build.VERSION.SDK_INT >= 19){
mContext.getContentResolver().takePersistableUriPermission(list.get(position), takeFlags);
}
is = mContext.getContentResolver().openInputStream(list.get(position));
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
bitmap = BitmapFactory.decodeStream(is,null, options);
is.close();
imageView.setImageBitmap(bitmap);
return imageView;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
我做错了什么?谢谢