'Can't intent in new activitys

I'm creating a file explorer synchronize with an ftp directory. I have recently adapted my app to work with both internal and external storage, and then I encounter an embarrassing issue. When I try to open a file using this function to intent a new activity:

    private fun openFile(file: File) {
        val myMime = MimeTypeMap.getSingleton()
        val intent = Intent(Intent.ACTION_VIEW)

        val mimeType = myMime.getMimeTypeFromExtension(file.extension)
        if (Build.VERSION.SDK_INT >= 24) {
            val fileURI = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID.toString() + ".provider", file)
            intent.setDataAndType(fileURI, mimeType)
        } else {
            intent.setDataAndType(Uri.fromFile(file), mimeType)
        }
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
        try {
            context.startActivity(intent)
        } catch (e: ActivityNotFoundException) {
            Toast.makeText(context, "No app found", Toast.LENGTH_LONG).show()
        }
    }

It thow me this error:

    java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/4B07-EB9F/Android/data/fr.frd.calvezdocs/files/CalvezDocs/ok/lol

I think it's related to my path provider, because when I try to open a file on the internal storage it simply work. Here it is but as you can see it's full, I don't know if a path is missing.

Here's my path provider:

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

    <external-path
        name="external_storage_root"
        path="Android/data/${applicationId}"/>

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

Someone have an idea?

Thanks



Solution 1:[1]

/storage/4B07-EB9F/Android/data/fr.frd.calvezdocs/files/CalvezDocs/ok/lol....

That is a path on a removable micro sd card.

FileProvider normally cannot handle such a path.

For not to high Android versions you can add to xml file:

<root
    name="root"
    path="." />

For devices with higher Android versions you should make your own content provider to serve from micro sd card.

It's not much code to do so.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1