I am trying to imlement downloading and sharing PDF file for read by another PDF reader apps (DropBox, Drive PDF Reader or Adobe Reader) on Android using FileProvider way.
However, I keep getting the following exception:
Failed to find configured root that contains /storage/emulated/0/Android/data/com.my.app/files/docs/my_file_name.pdf
I first send the URL to Android DownloadManager
to perform the downloading and then receive the result using BroadcastReceiver
My implementation as follow:
file_paths.xml
<paths>
<external-files-path
name="my_docs"
path="docs" />
</paths>
AndroidManifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
And from MainActivity:
public void downloadFile(String urlString) {
Uri uri = Uri.parse(urlString);
String filename = "my_file_name.pdf";
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
request.setTitle("App is Downloading a File");
request.setDescription("Downloading PDF File For App");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(mContext, "docs", filename);
request.allowScanningByMediaScanner();
mDownloadManager.enqueue(request);
}
And finally from my custom BroadcastReceiver:
uriString
is taken from a cursor.getString(uriIndex)
using downloadManager.query(myQuery)
the usual way.
private void startDocumentReaderUsingFileProvider(Context context, String uriString) {
Uri parsedUri = Uri.parse(uriString);
File file = new File(parsedUri.getPath());
String packageName = context.getApplicationContext().getPackageName();
// Exception thrown from the following line.
Uri uri = FileProvider.getUriForFile(context, packageName + ".fileprovider", file);
startActivity(context, uri);
}
private void startActivity(Context context, Uri uri) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent chooser = Intent.createChooser(intent, "Display PDF Reader");
context.startActivity(chooser);
}
Exception stack trace:
FATAL EXCEPTION: main
Process: com.my.app, PID: 8972
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.my.app bqHint=4 (has extras) } in com.my.app.FileBroadcastReceiver@82a2a7
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1003)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.my.app/files/docs/my_file_name.pdf
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)
at com.my.app.FileBroadcastReceiver.startDocumentReaderUsingFileProvider(FileBroadcastReceiver.java:85)
at com.my.app.FileBroadcastReceiver.startDocumentReader(FileBroadcastReceiver.java:69)
at com.my.app.FileBroadcastReceiver.onReceive(FileBroadcastReceiver.java:43)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:993)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
06-07 12:56:50.981 8972-9463/com.my.app E/com.worklight.common.Logger$UncaughtExceptionHandler: Logger$UncaughtExceptionHandler.uncaughtException in Logger.java:442 :: Uncaught Exception
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.DOWNLOAD_COMPLETE flg=0x10 pkg=com.my.app bqHint=4 (has extras) } in com.my.app.FileBroadcastReceiver@82a2a7
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:1003)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.my.app/files/docs/my_file_name.pdf
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:678)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:377)
at com.my.app.FileBroadcastReceiver.startDocumentReaderUsingFileProvider(FileBroadcastReceiver.java:85)
at com.my.app.FileBroadcastReceiver.startDocumentReader(FileBroadcastReceiver.java:69)
at com.my.app.FileBroadcastReceiver.onReceive(FileBroadcastReceiver.java:43)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:993)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
<paths>
<external-files-path
name="my_docs"
path="docs" />
</paths>
When you specify path as docs
the sub-directories are not included. You get the exception because its not expecting to share the subdirectories. To include them append /
at the end as follows:
<paths>
<external-files-path name="my_docs" path="docs/" />
</paths>