【发布时间】:2017-04-04 22:10:30
【问题描述】:
我从一个不同的问题开始,导致我来到这里,所以我只是从头开始。我有一个应用程序,允许用户为他们的客户创建报价。该应用程序为报价创建一个 pdf 文件并将其专门保存到外部存储中,以便其他应用程序可以访问它。最近,从我的应用程序中打开某些应用程序时,我似乎无法查看 pdf(谷歌 pdf 查看器、谷歌驱动器查看器显示空白屏幕,而 adobe acrobat 非常好)。我知道 pdf 很好,因为我可以从文件管理器中查看它而没有任何问题。只是从我的应用程序中打开它时它不起作用。
我提供了一个意图,其 uri 类似于 content://path/to/external/storage/MyApp/customerName/quote.pdf
我曾经以file://... 的形式提供它,但遇到了 sdk 24 中引入的暴露的 uri 异常,所以我只是将 file:// 重命名为 content:// 并且一切似乎都在工作,直到我发现了 google pdf 查看器应用程序。
现在我假设它与我显然应该使用的 fileProvider 类有关。但我似乎根本无法让它工作,而且我发现的有关该问题的其他问题似乎都没有工作,所以这就是我目前所拥有的:
file_paths.xml(尝试了几个不同的路径,似乎没有一个有效。还有用户创建的多个客户文件夹,我无法在 xml 文件中指定它们,因为我不知道它们将被命名,但是为了测试,我把一个放在那里只是为了看看这是否是问题,它仍然不起作用)
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="." />
<external-path name="myApp" path="MyApp/" />
<external-path name="myAppa" path="MyApp/customerFolder/" />
<external-path name="myAppb" path="/" />
</paths>
清单文件(其中一个包含使用的文件提供程序,这就是我有工具的原因:在其中替换行,我不知道这是否导致问题,它用于将照片保存到我试图从同一目录查看 pdf)
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="authorities"
>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"
tools:replace="android:resource"/>
</provider>
以及启动查看 pdf 的意图的页面:
String url = "file:///storage/emulated/0/MyApp/customerFolder/quote.pdf";
File mFile = new File(url);
Uri pdfURI = FileProvider.getUriForFile(activity, activity.getApplicationContext().getPackageName() + ".provider", mFile);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setData(pdfURI);
intent.setType("application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent);
这一直给我以下错误
java.lang.IllegalArgumentException: Failed to find configured root that contains /file:/storage/emulated/0/MyApp/customerFolder/quote.pdf
在包含FileProvider.getUriForFile() 方法的行上。我检查了路径,它是正确的,
或者,这是我曾经拥有的代码,它适用于 Adobe Acrobat,但不适用于 Google pdf 查看器或 Google Drive 查看器。如果有某种方法可以解决我缺少的问题,那么我可以忘记 fileProvider:
String url = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? "content://" : "file://") + "/storage/emulated/0/MyApp/customerFolder/quote.pdf";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url),"application/pdf");
activity.startActivity(intent);
【问题讨论】:
标签: android android-fileprovider