2

I am trying to allow the user to upload an image from the gallery.

On click of a button pickFromGallery -

   pickFromGallery.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (Build.VERSION.SDK_INT < 19) {
                        Intent intent = new Intent();
                        intent.setType("image/jpeg");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent, "Select Picture"), 2124);
                    } else {
                        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                        intent.addCategory(Intent.CATEGORY_OPENABLE);
                        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                        intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
                        intent.setType("image/jpeg");
                        startActivityForResult(intent, 1);
                    }
                }
            });

onActivityResult is implemented as so -

if (requestCode == 1) {
            Uri originalUri = null;
            if (Build.VERSION.SDK_INT < 19) {
                originalUri = data.getData();
            } else {
                originalUri = data.getData();
                final int takeFlags = data.getFlags()
                        & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                        | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

                try {
                    getContentResolver().takePersistableUriPermission(originalUri, takeFlags);
                }
                catch (SecurityException e){
                    e.printStackTrace();
                }
            }

And I get the exception -

11-02 19:17:08.848 24468-24468/com.tinystep.app W/System.err: java.lang.SecurityException: No persistable permission grants found for UID 10268 and Uri 0 @ file:///storage/emulated/0/DCIM/Camera/IMG_20150612_222143.jpg

ONLY on Xiomi Mi (Android Version 5.0.2)

My Manifest -

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.android.launcher.permission.wINSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="com.example.gcm.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>

I've looked at How to persist permission in android API 19 (KitKat)? and No permission for UID to access URI error on Android Application but the solutions mentioned there didn't work for me.

Any help would be highly appreciated, I've been stuck on this for a while.

Community
  • 1
  • 1
Mallika Khullar
  • 1,725
  • 3
  • 22
  • 37
  • 2
    The `Uri` in question is a `file:///` `Uri`, and I am not aware that there *are* persistable `Uri` permissions for those. Sounds like some `ACTION_OPEN_DOCUMENT` implementation is not following the instructions, as the `Uri` you get back should be a `content://` `Uri`, IIRC. – CommonsWare Nov 02 '15 at 14:33
  • Exactly you're right, I'm expecting a `content://` `Uri`. But the `Intent` I use opens up the entire folder structure of the phone, not just the gallery and lets me choose any file. Is there another way to just open the gallery? – Mallika Khullar Nov 02 '15 at 14:36
  • "But the Intent I use opens up the entire folder structure of the phone" -- it is supposed to open up the Storage Access Framework UI, which allows the user to pick a provider (for local files or for cloud-based stores like Dropbox or Google Drive). "Is there another way to just open the gallery?" -- no. Even your pre-19 code will not "just open the gallery". That being said, your pre-19 code should still work on Android 4.4+. – CommonsWare Nov 02 '15 at 14:40
  • Cool, I'll just use that for all versions then. Thanks! Wonder if I ever find what the problem was here. – Mallika Khullar Nov 02 '15 at 14:57

0 Answers0