Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android FileProvider for ext sdcard

I'm using FileProvider for my internal files to be exposed to the Gallery for example. To make it more uniform, I also put my my external files into the provider (via external-path) but for the files in removable sd card it doesn't work. Saying something like that folder is not authorized.

Any help will be greatly appreciated.

Thx

like image 413
stdout Avatar asked Sep 01 '15 13:09

stdout


2 Answers

i added this root-path as suggested by @Gubatron in my XML and it works.

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
    <root-path name="external_files" path="/storage/" />
</paths>
like image 163
Siddhesh Shirodkar Avatar answered Oct 20 '22 01:10

Siddhesh Shirodkar


Let's take a look at FileProvider code:

    private static PathStrategy parsePathStrategy(Context context, String authority)
        ...
        int type;
        while ((type = in.next()) != END_DOCUMENT) {
            if (type == START_TAG) {
                final String tag = in.getName();
                final String name = in.getAttributeValue(null, ATTR_NAME);
                String path = in.getAttributeValue(null, ATTR_PATH);
                File target = null;
                if (TAG_ROOT_PATH.equals(tag)) {
                    target = buildPath(DEVICE_ROOT, path);
                } else if (TAG_FILES_PATH.equals(tag)) {
                    target = buildPath(context.getFilesDir(), path);
                } else if (TAG_CACHE_PATH.equals(tag)) {
                    target = buildPath(context.getCacheDir(), path);
                } else if (TAG_EXTERNAL.equals(tag)) {
                    target = buildPath(Environment.getExternalStorageDirectory(), path);
                }
                if (target != null) {
                    strat.addRoot(name, target);
                }
            }
        }
        return strat;
    }

FileProvider accepted absolute pathes to directory with help of tag root-path (DEVICE_ROOT constant). So just add absolute path to your files folder in secondary external disc like below:

<root-path path="/storage/extSdCard/Android/data/com.edufii/files/image/" name="image-ext2" />
<root-path path="/storage/extSdCard/Android/data/com.edufii/files/video/" name="video-ext2" />
<root-path path="/storage/extSdCard/Android/data/com.edufii/files/datafile/" name="datafile-ext2" />
<root-path path="/storage/extSdCard/Android/data/com.edufii/files/audio/" name="audio-ext2" />

Note official documentation doesn't say anything about <root-path>, so it may changed in a future.

like image 20
danik Avatar answered Oct 19 '22 23:10

danik