0

when the user choosing a file it's triggers this code...

public static void openFiles(File file, Context context) {
        Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file);
        MimeTypeMap mimeType = MimeTypeMap.getSingleton();
        String type = mimeType.getMimeTypeFromExtension(file.getName().substring(file.getName().lastIndexOf(".") + 1).toLowerCase());
        //Toast.makeText(context, type, Toast.LENGTH_SHORT).show();
        if (type == null) type = "*/*";
        Intent share = new Intent(Intent.ACTION_VIEW);
        share.setDataAndType(uri, type);
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(share);
        //context.startActivity(Intent.createChooser(share, file.getName()));
    }

The Problem:

when i'm choosing a file from internal storage it's working fine, but when i try to open a file from external sd card i got this excaption

I/ViewRootImpl@f0cb2c4[MainActivity]: ViewPostIme pointer 0
I/ViewRootImpl@f0cb2c4[MainActivity]: ViewPostIme pointer 1
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.files, PID: 21495
    java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/5A85-D438/DCIM/Camera/20200406_100806.jpg
        at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744)
        at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418)
        at com.example.files.JFileAdapter.openFiles(MainActivity.java:596)
        at com.example.files.JFileAdapter.clickItem(MainActivity.java:579)
        at com.example.files.JFileAdapter.lambda$getView$0$JFileAdapter(MainActivity.java:501)
        at com.example.files.-$$Lambda$JFileAdapter$7O8geCTLRGHMEPzMgOfCRCbnWso.onClick(Unknown Source:8)
        at android.view.View.performClick(View.java:7862)
        at android.view.View.performClickInternal(View.java:7831)
        at android.view.View.access$3600(View.java:879)
        at android.view.View$PerformClick.run(View.java:29359)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8167)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
I/Process: Sending signal. PID: 21495 SIG: 9

res/xml/file_paths

<?xml version="1.0" encoding="utf-8"?>
<paths >
    <cache-path name="cache" path="." />
    <files-path name="files" path="." />
    <external-path name="files" path="." />
</paths>

Manifest

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

what i'm doing wrong that he faild to open an external sd card file?

J El
  • 100
  • 7
  • This looks somewhat relevant: https://stackoverflow.com/questions/42516126/fileprovider-illegalargumentexception-failed-to-find-configured-root –  Jan 13 '21 at 09:04

1 Answers1

1

FileProvider normally does not serve from a removable micro sd card.

But for Android 10- it will do if you add a line to your xml file:

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

You can even remove all other path declarations.

blackapps
  • 8,011
  • 2
  • 11
  • 25
  • The only thing i know is that it does nothing for an Android 11 device. But you can always download old FileProvider.java file and add it to your source. Or edit the new one accordingly. – blackapps Jan 13 '21 at 13:48
  • See: https://stackoverflow.com/questions/40318116/fileprovider-and-secondary-external-storage – blackapps Jan 13 '21 at 13:54