4

I want to open the downloads folder of external files directory by most of the file manager applications. For this I tried this:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS));
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setDataAndType(uri, "*/*");
startActivity(Intent.createChooser(intent, "Open folder"));

And I added this provider in manifest file:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
</provider>

Also there is provider_paths xml file content:

<paths>
    <external-files-path
        name="files"
        path="." />
</paths>

But it opens the recent folder of the default Android file picker.

Please pay attention that I want to open a folder not a file.

  • 4
    `FileProvider` does not serve directories, in part because `ContentProvider` does not serve directories. A `DocumentsProvider` does, because they layered a protocol on top of `ContentProvider` to make that work. Beyond that, `ACTION_GET_CONTENT` does not use a `Uri`, and if you want the user to choose a document tree, use `ACTION_OPEN_DOCUMENT_TREE` on Android 5.1+. – CommonsWare Jan 02 '18 at 13:45

0 Answers0