how to set FileProvider for file in External Cache dir

后端 未结 3 566
情深已故
情深已故 2020-12-16 20:54

For file in Cache dir , I can add this in xml to set FileProvider

    

But if I s

相关标签:
3条回答
  • 2020-12-16 21:36

    Following configuration works for me: I use

        <paths>
          <external-cache-path name="external_files" path="."/>
          <external-path name="external_files" path="."/>
        </paths>
    

    in my code.

    and my provider looks like this

     <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.bqe.core.provider.CameraFilesProvider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/camera_files_provider_paths" />
    

    my min sdk

               minSdkVersion 21
    
    0 讨论(0)
  • 2020-12-16 21:43

    Update:
    The way to go with the Support Library 24.2.0 and above is how @ashughes says in his answer.

    On that version they added two new tags to the implementation of FileProvider.java to be able to reference the external files and cache directories.

    private static final String TAG_EXTERNAL_FILES = "external-files-path";
    private static final String TAG_EXTERNAL_CACHE = "external-cache-path";
    

    If you want to dig more into it, you can take a look at the changes made on this commit.

    Original answer:
    I had the same problem. I took a look at the class FileProvider.java, and as you said, there is no tag for external cache dir, just the four below.

    private static final String TAG_ROOT_PATH = "root-path";
    private static final String TAG_FILES_PATH = "files-path";
    private static final String TAG_CACHE_PATH = "cache-path";
    private static final String TAG_EXTERNAL = "external-path";
    

    What I do is to use the external-path tag. This tag will point to the root of the external directory not to the cache one. So you can either specify the rest of the path from there to the cache directory on path, or you can use a dot so it points to the root of the external directory.

       <external-path
        name="external_files"
        path="."/>
    

    When you were using

    <external-path
        name="image_external"
        path="cache/image/"/>
    

    The method getFileForUri was checking if the path of the file starts with /storage/emulated/0/cache/image/ which it doesn't, because the path of your file is /storage/emulated/0/Android/data/com.tizi.quanzi/cache/image/. And that's the reason you are getting the exception.

    0 讨论(0)
  • 2020-12-16 21:45

    As of Support Library 24.2.0, you can use:

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

    See FileProvider docs for more details.

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