ダウンロードと共有を実装しようとしていますPDF他のユーザーが読み取るためのファイルPDFリーダーアプリ(DropBox、ドライブPDFリーダーまたはAdobe Reader)AndroidでFileProviderを使用して。
ただし、次の例外が発生し続けます。
_Failed to find configured root that contains /storage/emulated/0/Android/data/com.my.app/files/docs/my_file_name.pdf
_
最初にURLをAndroid DownloadManager
に送信してダウンロードを実行し、次にBroadcastReceiver
を使用して結果を受信します
次のように私の実装:
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>
_
および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);
}
_
そして最後に、私のカスタムBroadcastReceiver:から
uriString
は、通常の方法でcursor.getString(uriIndex)
を使用してdownloadManager.query(myQuery)
から取得されます。
_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);
}
_
例外スタックトレース:
_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>
パスをdocs
として指定すると、サブディレクトリは含まれません。サブディレクトリを共有することを期待していないため、例外が発生します。それらを含めるには、次のように末尾に/
を追加します。
<paths>
<external-files-path name="my_docs" path="docs/" />
</paths>