Picking an image file from Gallery using FileProvider

前端 未结 2 1060
耶瑟儿~
耶瑟儿~ 2021-02-18 18:34

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.

相关标签:
2条回答
  • 2021-02-18 19:23

    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.

    0 讨论(0)
  • 2021-02-18 19:27

    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.

    0 讨论(0)
提交回复
热议问题