简体   繁体   English

权限拒绝媒体文件提供者

[英]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 . 我使用此路径在RecyclerView显示图像。 I use Picasso to download the image in the ImageView . 我使用PicassoImageView下载图像。 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: 当我选择该图像正确显示在RecyclerView ,但是如果我离开屏幕并返回,则会出现此错误:

01-19 15:05:18.984 542-840/? 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 W / ActivityManager:权限拒绝:从...打开提供程序com.android.providers.media.MediaDocumentsProvider需要android.permission.MANAGE_DOCUMENTS或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 . 当您使用ACTION_GET_CONTENT您具有使用返回的Uri 临时权限

Also note that ACTION_GET_CONTENT does not take a Uri in the Intent . 另请注意, ACTION_GET_CONTENTIntent中不使用Uri Get rid of android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI . 摆脱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. 您正在尝试使用稍后返回的Uri The instance component (in this case, the activity) that receives the Uri has rights to use it, but nothing else in your app does. 接收到Uri的实例组件(在本例中为活动)有权使用它,但您的应用程序中没有其他操作。

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. 例如,假设活动A具有问题代码,然后在其中调用startActivity()启动活动B。默认情况下,活动B将无权访问Uri标识的内容。 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. 但是,活动A可以将FLAG_GRANT_READ_URI_PERMISSION标志添加到它与startActivity()一起使用的Intent ,并传递对活动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. 但是,其他形式的导航(例如BACK按钮)不提供此功能,更不用说将Uri保留为字符串并在明天或下周尝试使用它了。

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. 如果您在Android 4.4+上切换到ACTION_OPEN_DOCUMENT ,则可以尝试在ContentResolver上使用takePersistableUriPermission() ,在这种情况下,您可以永久访问该内容。

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 ). 否则,如果您正在寻找对内容的长期访问,则需要将内容(例如,复制映像)复制到您控制的存储器(例如, 内部存储器 )中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 权限拒绝:开放提供者 com.android.providers.media.MediaDocumentsProvider - Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider 拒绝权限:开放提供者 - Permission denial: opening provider java.lang.SecurityException:权限被拒绝:打开提供程序 com.android.providers.media.Media - java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.Media SecurityException:Permission Denial:打开提供程序 - SecurityException: Permission Denial: opening provider Android - 文件提供者 - 权限被拒绝 - Android - file provider - permission denial 日历提供程序:Android 8上的权限拒绝 - Calendar Provider: Permission Denial on Android 8 权限拒绝:重新启动应用程序时打开提供程序 com.android.providers.media.MediaDocumentsProvider - Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider when RESTARTING the app cordova-plugin-camera:权限被拒绝:读取com.android.providers.media.MediaDocumentsProvider需要android.permission.MANAGE_DOCUMENTS - cordova-plugin-camera: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider requires android.permission.MANAGE_DOCUMENTS 通过意图拒绝文件提供者的权限 - Permission Denial with File Provider through intent 自定义内容提供程序和权限拒绝例外 - Custom content provider and Permission Denial exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM