android 17 19,java - How to persist permission in android API 19 (KitKat)? - Stack Overflow

作者在应用中将图片路径存储在SQLite中,但在尝试从数据库获取路径并加载图片时遇到`SecurityException`。文章探讨了如何通过添加`takePersistableUriPermission`解决`PermissionDenial`,以及在ImageAdapter中正确处理URI权限的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In my application I store the path of image in my SQlite db for further use. The path that I get is

content://com.android.providers.media.documents/document/image%3A71964

When I retrieve this path from the database and try to retrieve the image from that path android throws

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

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);

When I added this code to my ImageAdapter class which extends 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

This is the relevant part of my ImageAdapter code

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;

}

what am I doing wrong?

Thanks

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值