在我的应用程序中,我将图像路径存储在我的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
这是我的ImageAdapter代码的相关部分
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;
}
我做错了什么?
感谢
答案 0 :(得分:25)
我相信我已经解决了。请求意图:
Intent intent;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
}else{
intent = new Intent(Intent.ACTION_GET_CONTENT);
}
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.form_pick_photos)), REQUEST_PICK_PHOTO);
和onActivityResult
...
// kitkat fixed (broke) content access; to keep the URIs valid over restarts need to persist access permission
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;
ContentResolver resolver = getActivity().getContentResolver();
for (Uri uri : images) {
resolver.takePersistableUriPermission(uri, takeFlags);
}
}
...
我还没有测试过这款pre-kitkat,我的手机正在运行5.1,任何人都可以在旧手机上验证这一点吗?
答案 1 :(得分:0)
下面列出的代码,
// kitkat fixed (broke) content access; to keep the URIs valid over restarts need to persist access permission
if(Utils.isKitkat())
{
final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION);
ContentResolver resolver = getActivity().getContentResolver();
for (Uri uri : images)
{
resolver.takePersistableUriPermission(uri, takeFlags);
}
}
对我来说很好,无论如何我注意到我的应用程序授予的最大可持续uri数量限制为128.如果我选择超过128 uri,我会收到错误:
java.lang.SecurityException: Permission Denial: opening provider........
当我尝试处理我无法持久许可的图像时。你能找出任何解决方案吗?
答案 2 :(得分:-1)
避免请求权限的其他方法是使用SharedPreferences。将uri保存为字符串(或其他您需要的方式)并毫无困难地检索它。