【发布时间】:2018-05-16 11:35:20
【问题描述】:
我尝试在 Android 平台上的应用中打开一个文件。我用:
let intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
let fileURI = android.net.Uri.fromFile(new java.io.File(file.path));
intent.setDataAndType(fileURI, "application/pdf");
let activity = android.content.Intent.createChooser(intent, `Open Document with :`);
app.android.currentContext.startActivity(activity);
它适用于 android 版本 7.1.1 及之前的版本。但是在 8.0 版上。我有这样的错误:
android.os.FileUriExposedException: file:///data/user/0/... exposed beyond app through Intent.getData()
所以,我尝试使用 android.support.v4.content.FileProvider 解决问题。
我将代码修改为:
let intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
intent.addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION);
let context = application.android.currentContext;
let nativeFile = new java.io.File(file.path);
var uri = new android.support.v4.content.FileProvider.getUriForFile(context,"${applicationId}.provider,nativeFile",nativeFile);
intent.setDataAndType(uri, "application/pdf");
application.android.currentContext.startActivity(android.content.Intent.createChooser(intent, "Open File...");
我修改了 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>
我在文件夹 App_Ressources/Android/xml 中创建了一个名为 provider_paths.xml 的文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
我删除并添加平台。 但我无法运行代码,因为我仍然有错误:
Property 'FileProvider' does not exist on type 'typeof content'
【问题讨论】:
-
I try to open a file in my app。是吗?它看起来更像是你试图让它被另一个应用程序打开。 -
/data/user/0/...那是内部路径。所以你不能使用<external-path ...。 -
context,"${applicationId}.provider不匹配android:authorities="${applicationId}.fileprovider"。 -
@greenapps 我下载了感谢 http.getFile() 然后,我想看看它(pdf)。
-
@greenapps 好的,这是内部路径,我该怎么办?
标签: android angular2-nativescript android-fileprovider