使用FileProvider从Gallery中选择图像文件 [英] Picking an image file from Gallery using FileProvider

查看:57
本文介绍了使用FileProvider从Gallery中选择图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

针对Android N进行编译我遇到了FileProvider的问题.我需要让用户从图库中选择图像/使用相机拍照,然后将其裁剪为正方形.

Compiling for Android N I've faced an issue of FileProvider. I need to let user to pick image from gallery/take picture with camera then crop it to square.

我设法实现了FileProvider以便用相机拍摄图像,但是从图库中拾取图像时我遇到了严重的问题.问题是在图库中有很多来自不同地方的文件,例如,我有例外:

I've managed to implement a FileProvider for taking image with camera, but I have serious problem with picking image from gallery. The problem is that in the gallery there are lot of files from different places and I've got the Exception for example:

  java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/6133-3766/DCIM/100ANDRO/DSC_0035.JPG

所以问题是,我可以放入 file_paths.xml 以访问/storage/中的任何位置.我不能依靠确切的路径,因为可能有来自WhatsApp和类似应用程序的图片,例如WhatsApp图像获得了以下路径:

So the question is, what can I put to file_paths.xml to get access to anywhere in /storage/. I can't rely on exact path, since there maybe pictures from WhatsApp and similar apps, for example the WhatsApp image gets this path:

/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20160821-WA0000.jpg

我设法用空路径解决了该问题:

which I've managed to resolve with empty path:

<external-path name="external_storage" path=""/>

<external-path name="external_storage" path=""/>

Environment.getExternalStorageDirectory()类似,根据文档.

但是仍然无法弄清楚如何处理/storage/SOME_DIR/中存储的图像.请帮忙.

But still cannot figure out how to deal with images that stored in /storage/SOME_DIR/. Please help.

推荐答案

我认为这个问题是基于误解.

I think this question is based on a misunderstanding.

FileProvider 的目的是(授予外部应用程序)对您的应用程序已控制的文件的访问权限.

The purpose of a FileProvider is to grant access (to an external app), to a file that your app already controls.

您将永远无法成功使用自己的文件提供程序来获得对外部应用拥有的文件的访问权限.

You will never succeed in using your own file provider to gain access to a file owned by an external app.

由外部应用程序(如果选择的话)授予您使用文件提供者的访问权限.

It is up to the external app to grant you that access using a file provider, if it chooses to.

这似乎是问题所在.如果我不明白您的问题,请告诉我,但是如果我确实理解了,您尝试执行的操作将无法正常工作.

That seems to be what the question is asking for. If I have not understood your question, let me know, but if I do understand it, what you are trying to do just won't work.

这篇关于使用FileProvider从Gallery中选择图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆