简体   繁体   English

从外部存储打开图像

[英]Open an image from the external storage

I've been trying for several hours to open an image from Android's external storage with Intent.ACTION_VIEW . 我已经尝试了几个小时,使用Intent.ACTION_VIEW从Android的外部存储中打开图像。 And as you might have figured out already, I'm still not able to do that. 正如您可能已经知道的那样,我仍然无法做到这一点。

I've searched all over Google and I've tried a lot of ways to make this work, however none of them did. 我在Google各处进行了搜索,并尝试了很多方法来完成这项工作,但是没有一个方法能做到。 All what I could find are questions that are up to 1 year old. 我所能找到的都是长达1年的问题。 I tried using FileProvider and here's what I ended up with: 我尝试使用FileProvider ,最终得到的是:

File imagePath = new File(Environment.getExternalStorageDirectory(), "LyceeLamartine/cache");
File newFile = new File(imagePath, "calendrier.png");
Uri contentUri = FileProvider.getUriForFile(MainActivity.this, "ml.toufic.lyceelamartine.fileprovider", newFile);

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(contentUri, "image/*");
startActivity(intent);

The code above opens Google Photos successfully but it shows a black screen and the path seems to be: content://ml.toufic.lyceelamartine.fileprovider/name/calendrier.png and this seems like an internal storage path. 上面的代码成功打开了Google相册,但显示黑屏,其路径似乎为: content://ml.toufic.lyceelamartine.fileprovider/name/calendrier.png ,这似乎是内部存储路径。

The image I want to access is stored at: "/storage/emulated/0/LyceeLamartine/cache/calendrier.png" and I'm using a phone with Android O (8.0). 我要访问的图像存储在:“ / storage / emulated / 0 / LyceeLamartine / cache / calendrier.png”,并且我正在使用具有Android O(8.0)的手机。

I'm still a beginner in Java and any help would be greatly appreciated! 我仍然是Java的初学者,任何帮助将不胜感激!

the path seems to be: content://ml.toufic.lyceelamartine.fileprovider/name/calendrier.png and this seems like an internal storage path. 路径似乎是:content://ml.toufic.lyceelamartine.fileprovider/name/calendrier.png,这似乎是内部存储路径。

That is not a path. 那不是一条路。 That is a Uri . 那是一个Uri It has a scheme ( content ). 它有一个方案( content )。 It points to your FileProvider ( ml.toufic.lyceelamartine.fileprovider ). 它指向您的FileProviderml.toufic.lyceelamartine.fileprovider )。 It seems fine — if there were a problem with the Uri , FileProvider would have thrown an exception when you tried to create it. 看起来很好-如果Uri出现问题,则FileProvider在尝试创建它时会引发异常。

The code above opens Google Photos successfully but it shows a black screen 上面的代码成功打开了Google相册,但显示黑屏

There are two problems with your Intent . 您的Intent有两个问题。

First, do not use a wildcard MIME type. 首先,不要使用通配符MIME类型。 It is your file. 这是您的文件。 You know what the MIME type is. 知道什么是MIME类型。 Use the actual MIME type (in this case, image/png ). 使用实际的MIME类型(在本例中为image/png )。

Second, call addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) on the Intent as part of setting it up, before calling startActivity() . 其次,在调用startActivity()之前,在Intent上调用addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)作为设置它的一部分。 Right now, no app has rights to use your content. 目前,没有任何应用有权使用您的内容。

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

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