Picking an image file from Gallery using FileProvider

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-13 07:42:10

问题


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.

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

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=""/>

which is similar to Environment.getExternalStorageDirectory() according to documentation.

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


回答1:


I think this question is based on a misunderstanding.

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.




回答2:


Agreed with @x-code's answer you are not described very clear about issue although if you try to access another app's internal data then you must have permissions to do so.

Files that rightfully belong to your app and should be deleted when the user uninstalls your app. Although these files are technically accessible by the user and other apps because they are on the external storage, they are files that realistically don't provide value to the user outside your app.

Actually i have found on documentation that SDK version 24 is now updated with many schemes and have massive changes in working with Files,from documentation the problem with file:// is described as..

Passing file:// URIs outside the package domain may leave the receiver with an unaccessible path. Therefore, attempts to pass a file:// URI trigger a FileUriExposedException. The recommended way to share the content of a private file is using the FileProvider.

Due to security reasons it is highly recommended to use Content:// instead of using file:// so basically use ContentProvider instead of FileProvider.

A simple example of using it is below,

in AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>
</manifest> 

And then create a provider_paths.xml file in xml folder under res folder. Folder may be needed to create if it doesn't exist.

The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder (path=".") with the name external_files.

res/xml/provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

now to use it,

Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
        BuildConfig.APPLICATION_ID + ".provider",
        createImageFile());

I have taken this from this blog so please read it for full understanding.Hope it helps everyone.



来源:https://stackoverflow.com/questions/39097181/picking-an-image-file-from-gallery-using-fileprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!