Permission Denial 媒体文档提供者
Permission Denial media documents provider
我想从图库中选择一张图片,然后保存路径。我使用此路径在 RecyclerView
中显示图像。我使用 Picasso
下载 ImageView
中的图像。我遇到的问题如下:
当我选择图像时 RecyclerView
中正确显示,但是如果我离开屏幕再回来,我得到这个错误:
01-19 15:05:18.984 542-840/? W/ActivityManager:权限被拒绝:从...打开提供商 com.android.providers.media.MediaDocumentsProvider 需要 android.permission.MANAGE_DOCUMENTS 或 android.permission.MANAGE_DOCUMENTS
图像不再显示。这是我的代码:
<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);
}
}
有人知道问题出在哪里吗?提前致谢!
I want to choose a picture from the gallery and then save the path
那是行不通的。当您使用 ACTION_GET_CONTENT
、you have temporary rights to use the Uri
that you get back 时。
另请注意,ACTION_GET_CONTENT
不会在 Intent
中使用 Uri
。去掉 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
.
Does someone know where is the problem?
您正在尝试使用稍后取回的 Uri
。接收 Uri
的实例组件(在本例中为 activity)有权使用它,但您应用中的其他任何组件都没有。
在某些情况下,您可以将您的访问权限传播到其他组件实例。例如,假设 Activity A 拥有您问题中的代码,并且您在其中调用 startActivity()
以启动 Activity B。Activity B 将无法访问已识别的内容默认为 Uri
。但是,Activity A 可以将 FLAG_GRANT_READ_URI_PERMISSION
标志添加到它与 startActivity()
一起使用的 Intent
,并将读取权限传递给 Activity B。
但是,其他形式的导航(例如“后退”按钮)不提供此功能,更不用说将 Uri
作为字符串保存并在明天或下周尝试使用它了。
如果您在 Android 4.4+ 上切换到 ACTION_OPEN_DOCUMENT
,您可以尝试在 ContentResolver
上使用 takePersistableUriPermission()
,在这种情况下您可能会获得持久访问权内容。
否则,如果您要 long-term 访问内容,则需要将内容(例如,复制图像)复制到您控制的存储空间(例如,internal storage)。
我想从图库中选择一张图片,然后保存路径。我使用此路径在 RecyclerView
中显示图像。我使用 Picasso
下载 ImageView
中的图像。我遇到的问题如下:
当我选择图像时 RecyclerView
中正确显示,但是如果我离开屏幕再回来,我得到这个错误:
01-19 15:05:18.984 542-840/? W/ActivityManager:权限被拒绝:从...打开提供商 com.android.providers.media.MediaDocumentsProvider 需要 android.permission.MANAGE_DOCUMENTS 或 android.permission.MANAGE_DOCUMENTS
图像不再显示。这是我的代码:
<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);
}
}
有人知道问题出在哪里吗?提前致谢!
I want to choose a picture from the gallery and then save the path
那是行不通的。当您使用 ACTION_GET_CONTENT
、you have temporary rights to use the Uri
that you get back 时。
另请注意,ACTION_GET_CONTENT
不会在 Intent
中使用 Uri
。去掉 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI
.
Does someone know where is the problem?
您正在尝试使用稍后取回的 Uri
。接收 Uri
的实例组件(在本例中为 activity)有权使用它,但您应用中的其他任何组件都没有。
在某些情况下,您可以将您的访问权限传播到其他组件实例。例如,假设 Activity A 拥有您问题中的代码,并且您在其中调用 startActivity()
以启动 Activity B。Activity B 将无法访问已识别的内容默认为 Uri
。但是,Activity A 可以将 FLAG_GRANT_READ_URI_PERMISSION
标志添加到它与 startActivity()
一起使用的 Intent
,并将读取权限传递给 Activity B。
但是,其他形式的导航(例如“后退”按钮)不提供此功能,更不用说将 Uri
作为字符串保存并在明天或下周尝试使用它了。
如果您在 Android 4.4+ 上切换到 ACTION_OPEN_DOCUMENT
,您可以尝试在 ContentResolver
上使用 takePersistableUriPermission()
,在这种情况下您可能会获得持久访问权内容。
否则,如果您要 long-term 访问内容,则需要将内容(例如,复制图像)复制到您控制的存储空间(例如,internal storage)。