简体   繁体   中英

Permission Denial media documents provider

I want to choose a picture from the gallery and then save the path. I use this path to show the images in a RecyclerView . I use Picasso to download the image in the ImageView . The problem I have is the following:

when I choose the image is correctly shown in the RecyclerView , but if I leave the screen and come back, I get this error:

01-19 15:05:18.984 542-840/? W/ActivityManager: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ... requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS

and the image is not shown anymore. This is my code:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>


 AlertDialog.Builder builder = new AlertDialog.Builder(getMainActivity());
    builder.setTitle("Choose Image Source");
    builder.setItems(new CharSequence[] {"Gallery"},
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                            Intent intent;
                            // GET IMAGE FROM THE GALLERY
                            intent = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            intent.setType("image/*");
                            intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);   // Show local files only (available since honeycomb, sdkVersion 11)

                            startActivityForResult(Intent.createChooser(intent, "Choose a picture"), Constants.REQUEST_CHOOSE_PHOTO);
                }
            });

    builder.show();


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    ImageDb image = new ImageDb();
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {  
            case Constants.REQUEST_CHOOSE_PHOTO:
                image.setImageFilePath(data.getData().toString());

                break;
        }
        images.add(image);
    }
}


 @BindingAdapter({"url", "size"})
public static void loadImage(ImageView imageView, String url, float size) {
    if (!Strings.isNullOrEmpty(url)) {
        Picasso.with(imageView.getContext()).load(url).resize((int) size, (int) size).centerCrop().into(imageView);
    }
}

Does someone know where is the problem??Thanks in advance!

I want to choose a picture from the gallery and then save the path

That is not going to work. When you use ACTION_GET_CONTENT , you have temporary rights to use the Uri that you get back .

Also note that ACTION_GET_CONTENT does not take a Uri in the Intent . Get rid of android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI .

Does someone know where is the problem?

You are attempting to use the Uri that you get back later. The instance component (in this case, the activity) that receives the Uri has rights to use it, but nothing else in your app does.

In some situations, you can propagate your access rights to other component instances. For example, suppose Activity A has the code from your question, and in there you call startActivity() to start Activity B. Activity B will not have access to the content identified by the Uri by default. But, Activity A could add the FLAG_GRANT_READ_URI_PERMISSION flag to the Intent it uses with startActivity() , and pass along read access to Activity B.

But, other forms of navigation (eg, BACK button) do not offer this, let alone persisting the Uri as a string and trying to use it tomorrow or next week.

If you switch to ACTION_OPEN_DOCUMENT on Android 4.4+, you can try to use takePersistableUriPermission() on ContentResolver , in which case you may get durable access to that content.

Otherwise, if you are looking for long-term access to the content, you need to copy the content (eg, copy the image) into storage that you control (eg, internal storage ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM