How to save permission in API API 19 (KitKat)?

In my application, I save the image path in my SQlite db for future reference. The way that I get

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

When I get this path from the database and try to get an image from this path, the browser rolls

 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 

According to https://developer.android.com/guide/topics/providers/document-provider.html#permissions I need to save permission by adding the following code

 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 browsers for Android BaseAdapter adapters

 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 an important 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

+13
java android sqlite android-4.4-kitkat storage-access-framework
Aug 20 '14 at 21:01
source share
3 answers

I think I decided. Request Purpose:

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

and 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); } } ... 

I have not tested this pre-kitkat, my phone is running 5.1, can anyone test this on older phones?

+20
Apr 12 '15 at 10:48
source share

Below is the code,

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

worked fine for me, in any case, I noticed that the maximum number of saved uri provided to my application is limited to 128. If I select more than 128 uri, I get an error:

 java.lang.SecurityException: Permission Denial: opening provider........ 

when I try to process an image for which I could not save the resolution. Can you find a solution?

0
Jun 11 '15 at 19:08
source share

Another way to avoid query permission is to use SharedPreferences. save uri as a string (or another way you need) and get it without any problems.

0
Oct 01 '17 at 15:57
source share



All Articles