androidandroid-studioandroid-fileprovider

Failed to find configured root that contains /storage/3A8D-1922/Android/data/com.testing.new/files/releases/2.1.1.apk


I am so confused with FileProvider path. 3 days trying to figure out how this thing works. Already checks similar question and improvised, but still getting these error.

Failed to find configured root that contains /storage/3A8D-1922/Android/data/com.testing.new/files/releases/2.1.1.apk

Below is my provider_paths.xml.

   <external-files-path
        name="releases"
        path="Android/data/com.testing.new/files/releases" />

And in manifest

  <provider
            android:name=".Utility.Provider"
            android:authorities="com.testing.new.fileprovider"
            android:exported="false"
            android:enabled="true"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths" />
        </provider>

And below is how i am trying to open the file using fileprovider

 protected void openFile(String latestver) {
        File file1 = getApplicationContext().getExternalFilesDirs(null)[1];
        File file = new File(new File(file1, "releases"), latestver); 
        //releases = latestver..which is "2.1.1.apk"
        Log.d("fileeee", file.getAbsolutePath().toString());
        Uri data = Provider.getUriForFile(MainActivity.this, "com.testing.new.fileprovider", file);
        Intent install = new Intent(Intent.ACTION_VIEW);

        install.setDataAndType(data, "application/vnd.android.package-archive");

        install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        startActivity(install);
       
    }

Solution

    1. FileProvider normally cannot serve files from removable micro-sd card.

    2. <external-files-path should be used with getExternalFilesDir() or -which is the same- getExternalFilesDirs()[0].

    3. If you wanna serve from micro-sd card try <root and path=".".

    4. Use file.exists() and file.canRead() before you try to get an url.