Issue
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 get this exception
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?
Solution
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.
Answered By - blackapps
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.