简体   繁体   English

从持久性Uri加载图像时出错

[英]Error loading image from presistent Uri

The program flow is that the user selects an image from the gallery and it is displayed in an image view. 程序流程是用户从图库中选择一个图像,并在图像视图中显示该图像。 This works fine. 这很好。 At the same time, the Uri is written to a database. 同时,将Uri写入数据库。

Later, when the program is run again or that activity is displayed again, the Uri is retrieved from the database and the image is displayed again. 稍后,当再次运行该程序或再次显示该活动时,将从数据库中检索Uri,并再次显示图像。 This second showing of the image is what causes the exception: 图像的第二个显示是导致异常的原因:

requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS

I added the permission to the manifest and the exception still occurs. 我向清单添加了权限,但仍然发生异常。

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

The same procedure attempts to display both images, and it works when it comes from the picker, but fails when it comes from the presistent Uri from the database. 相同的过程尝试同时显示两个图像,当它来自选择器时可以工作,但是当它来自数据库中的持久Uri时失败。

Picker Intent 选择器意图

    try {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(
                Intent.createChooser(intent, "Select Picture"),
                PICK_IMAGE);
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                "Error",
                Toast.LENGTH_LONG).show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
    }

With the result from the picker intent I use 根据选择器意图的结果,我使用了

Uri selectedImageUri = data.getData();

With the database read I use. 随着数据库读取我使用。 localImage is a String. localImage是一个字符串。

Uri.parse(localImage)

I read this bug report https://issues.apache.org/jira/browse/CB-5398 , but I'm not sure how to use it to resolve this situation. 我阅读了此错误报告https://issues.apache.org/jira/browse/CB-5398 ,但是我不确定如何使用它来解决这种情况。 The code below from another thread seems to attempt to resolve a similar issue with permissions and the photo picker, but in my case I'm not picking again, I just need to display the image from the Uri. 下面来自另一个线程的代码似乎试图解决权限和照片选择器的类似问题,但就我而言,我不再选择,我只需要显示Uri中的图像即可。

I guess it is a matter of resetting permissions. 我想这是重置权限的问题。 I'm not really sure. 我不太确定 I have the permissions in the manifest. 我有清单中的权限。 Perhaps I need different code to display an image from a Uri not obtained from the picker? 也许我需要不同的代码来显示未从选择器获得的Uri图像? Maybe I need to target a different API? 也许我需要针对其他API? My build.gradle says targetSdkVersion 21, minSdkVersion 15. These were the defaults. 我的build.gradle表示targetSdkVersion 21,minSdkVersion15。这些是默认设置。 I'm in Android Studio 1.0.2. 我在Android Studio 1.0.2中。

I'm stumped. 我很沮丧 Any help is appreciated. 任何帮助表示赞赏。

public static final String 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
         }
      }
    }

At the same time, the Uri is written to a database. 同时,将Uri写入数据库。

That's not going to work prior to API Level 19, and then only if you are using the Storage Access Framework ( ACTION_OPEN_DOCUMENT ) and take the offered persistent permissions. 只有在您使用存储访问框架( ACTION_OPEN_DOCUMENT )并获取提供的持久权限时,API级别19之前的版本才能使用。 Given your existing code, the temporary permissions that you are granted for that Uri will expire, at the latest when your process is terminated. 给定您现有的代码,为此Uri授予的临时权限最迟将在过程终止时到期。

I added the permission to the manifest and the exception still occurs. 我向清单添加了权限,但仍然发生异常。

You cannot hold MANAGE_DOCUMENTS , unless you are signed by the signing key that signed the firmware. 您不能持有MANAGE_DOCUMENTS ,除非您通过对固件进行签名的签名密钥进行了签名。

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

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