Exception java.lang.SecurityException: reading ..MediaDocumentsProvider … requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()

后端 未结 2 1889
无人及你
无人及你 2020-12-31 19:30

I found this issue only in some devices when trying to pick the picture for the profile image. While checking in the emulator these issue are not seen but on live version of

相关标签:
2条回答
  • 2020-12-31 19:52

    Try this:

    public static final int KITKAT_VALUE = 1002;
    
    Intent intent;
    
    if (Build.VERSION.SDK_INT < 19) {
        intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");
        startActivityForResult(intent, KITKAT_VALUE);
    } else {
        intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        startActivityForResult(intent, KITKAT_VALUE);
    }
    
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == KITKAT_VALUE ) {
            if (resultCode == Activity.RESULT_OK) {
                // do something here
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-31 19:58

    I am trying to persist the Uri and use that Uri later

    That will work for a Uri with a file scheme. It will not work for most Uri values with a content scheme. Those are more akin to an HTTP URL to an authenticated Web site — the URL is good while your session is alive but is useless afterwards.

    If you obtain the Uri via ACTION_OPEN_DOCUMENT, ACTION_CREATE_DOCUMENT, or ACTION_OPEN_DOCUMENT_TREE, and you call takePersistableUriPermission() on ContentResolver supplying the Uri, then you can safely persist that Uri. You will have access to the content indefinitely, until the user revokes access or the content significantly changes (e.g., is deleted) such that its Uri is no longer valid.

    Any other content Uri (e.g., ACTION_GET_CONTENT) is only good for a very short time. Your only option here is to copy the content to some file that you control (e.g., in getCacheDir()) and save the path to that file.

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