【问题标题】:Property 'FileProvider' does not exist on type 'typeof content' : Nativescript with Angular on Android platform“typeof content”类型上不存在属性“FileProvider”:Android 平台上带有 Angular 的 Nativescript
【发布时间】: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'

click here to see the the capture

【问题讨论】:

  • I try to open a file in my app。是吗?它看起来更像是你试图让它被另一个应用程序打开。
  • /data/user/0/... 那是内部路径。所以你不能使用&lt;external-path ...
  • context,"${applicationId}.provider 不匹配 android:authorities="${applicationId}.fileprovider"
  • @greenapps 我下载了感谢 http.getFile() 然后,我想看看它(pdf)。
  • @greenapps 好的,这是内部路径,我该怎么办?

标签: android angular2-nativescript android-fileprovider


【解决方案1】:

我参加聚会有点晚了,但对于遇到这个问题的其他人 -

将此添加到您的 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    <application
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>
</manifest>

然后在App_Resources/Android/xml 文件夹中创建一个provider_paths.xml 文件。如果文件夹不存在则创建,然后在下面添加sn-p:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

OP 问题中代码的问题之一是在静态引用上使用了 new 关键字,并且似乎是意外复制/粘贴:

var uri = new android.support.v4.content.FileProvider.getUriForFile(context,"${applicationId}.provider,nativeFile",nativeFile);

你真正想要的是:

(在 TypeScript 中)

const uri = android.support.v4.content.FileProvider.getUriForFile(context, `${application.android.context.getPackageName()}.provider`, nativeFile);

剩下的代码没问题。

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2020-10-24
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 2020-08-19
    相关资源
    最近更新 更多