How to persist permission in android API 19 (KitKat)?

前端 未结 2 934
情深已故
情深已故 2020-11-29 03:27

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         


        
相关标签:
2条回答
  • 2020-11-29 03:38

    The code listed below,

    // 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, anyway I noticed that the max number of persistable uri granted to my app is limited to 128. If I select more than 128 uri, I get the error:

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

    just when I try to process an image for which I wasn't able to persist the permission. Can you figure out any solution?

    0 讨论(0)
  • 2020-11-29 03:43

    I believe I've solved it. The request intent:

    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 haven't tested this pre-kitkat, my phone is running 5.1, can anyone verify this on older phones?

    0 讨论(0)
提交回复
热议问题