我试图嵌入下载和共享PDF文件,由另一个PDF阅读器应用程序(DropBox,驱动器PDF阅读器或Adobe阅读器)在Android上使用FileProvider的方式。
然而,我一直得到以下例外:
Failed to find configured root that contains /storage/emulated/0/Android/data/com.my.app/files/docs/my_file_name.pdf
我首先将URL发送到AndroidDownloadManager
执行下载,然后使用BroadcastReceiver
我的执行情况如下:
file_paths.xml
<paths>
<external-files-path
name="my_docs"
path="docs" />
</paths>
Androidanifest.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>
主要活动:
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);
}
最后,从我的自定义广播接收器:
uriString
取自光标。getString(uriIndex)
使用下载管理器。查询(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>
我有一个关于android文件提供商的问题。我想保存一个pdf文档并用默认程序打开它。我不想将其保存在外部存储器中。 在我成功地将pdf保存到filedirectory/export/temp之后。pdf, 我尝试使用FileProvider生成URI。getUriForFile()。 问题:我必须传递什么作为第二个参数“Authority”-我的文件的位置、可以授予URI权限的类或其他什么?无论
编辑:好的,我已经尝试了这些建议,并将其更改为getExternalFilesDir(),但仍然收到相同的错误。跳到底部显示“已编辑代码”的地方,查看它现在是什么。我还更改了它,这样屏幕截图将保存到图片目录,而不是创建一个新目录。(结束编辑) 我有一个android应用程序,其中包含一个recyclerview。我已经创建了一个按钮,该按钮将导出并创建recyclerview数据的PNG,将其保存
我试图保存图像到内部存储,我面临: Java代码: 我的路径代码: 我的清单代码: 这上面有什么线索吗?
寻找一些错误的帮助,我正在尝试存储我正在尝试开发的应用程序的相机拍摄的照片。错误是 JAVAlang.IllegalArgumentException:未能找到包含/storage/emulated/0/Pictures/JPEG20161108_153704的已配置根目录_ logcat在我的代码中FileProvider所在的行中指向这个方法。正在调用getUriForFile。。 此方法用于
我试图通过Instagram的意图共享文件,但我意识到在API 24上,我不能只共享URI到其他应用程序没有给它的权限。 此后https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en在本教程中,我设置了提供程序并提供如下路径: 清单 注意:有“tool
我试图发送使用tcp套接字的文件列表,但我得到这个文件提供商错误。谢啦 原因:java。lang.IllegalArgumentException:未能找到包含 文件路径 文件路径。xml manifest.xml: