离子角电容器 - 未找到 FileOpener 活动:未找到处理意图的活动

Posted

技术标签:

【中文标题】离子角电容器 - 未找到 FileOpener 活动:未找到处理意图的活动【英文标题】:Ionic Angular Capacitor - FileOpener Activity Not Found: No Activity found to handle intent 【发布时间】:2021-12-22 12:53:33 【问题描述】:

我有一个 Angular 12 Capacitor 应用程序,我试图在用户选择的任何本机应用程序中打开下载的文件。

我正在使用 ionic-native/File 和 ionic-native/FileOpener

const dataType = event.body.type;
          const binaryData = [];
          binaryData.push(event.body);

          if (this.platform.is('mobile') && this.platform.is('capacitor')) 
            // running native mobile, try and open file
            console.log('mobile open file');
            const blob = new Blob(binaryData,  type: dataType );
            this.file.writeFile(this.file.dataDirectory, attachment.name, blob, replace: true).then(() => 
              console.log('file should be saved');
              this.fileOpener.open(this.file.dataDirectory + attachment.name, dataType).then(() => console.log('file should be open'));
            );
           else 
            // not running native mobile, download the attachment
            const downloadLink = document.createElement('a');
            downloadLink.href = window.URL.createObjectURL(new Blob(binaryData,  type: dataType ));
            downloadLink.setAttribute('download', attachment.name);
            downloadLink.setAttribute('target', '_self');
            document.body.appendChild(downloadLink);
            downloadLink.click();
          

但是,每次调用 fileopener.open 时,我都会收到一条错误消息:未找到活动:未找到处理意图的活动。以下是日志

I/Capacitor/Console: File: http://localhost/main.js - Line 3580 - Msg: mobile open file
V/Capacitor/Plugin: To native (Cordova plugin): callbackId: File189568326, service: File, action: resolveLocalFileSystemURI, actionArgs: ["file:\/\/\/data\/user\/0\/com.company.appname\/files\/"]
V/Capacitor/Plugin: To native (Cordova plugin): callbackId: File189568327, service: File, action: getFile, actionArgs: ["cdvfile:\/\/localhost\/files\/","1635956966683.jpeg","create":true,"exclusive":false]
V/Capacitor/Plugin: To native (Cordova plugin): callbackId: File189568328, service: File, action: getFileMetadata, actionArgs: ["cdvfile:\/\/localhost\/files\/1635956966683.jpeg"]
V/Capacitor/Plugin: To native (Cordova plugin): callbackId: File189568329, service: File, action: write, actionArgs: ["cdvfile:\/\/localhost\/files\/1635956966683.jpeg","data removed"
I/Capacitor/Console: File: http://localhost/main.js - Line 3583 - Msg: file should be saved
V/Capacitor/Plugin: To native (Cordova plugin): callbackId: FileOpener2189568330, service: FileOpener2, action: open, actionArgs: ["file:\/\/\/data\/user\/0\/com.company.appname\/files\/1635956966683.jpeg","application\/octet-stream"]
E/Capacitor/Console: File: http://localhost/vendor.js - Line 48142 - Msg: ERROR Error: Uncaught (in promise): Object: "status":9,"message":"Activity not found: No Activity found to handle Intent  act=android.intent.action.VIEW dat=content://com.company.appname.fileOpener2.provider/files/1635956966683.jpeg typ=application/octet-stream flg=0x3 "

我的假设是我需要一些用于添加到我的 AndroidManifest.xml 中的 android.intent.action.VIEW 的意图过滤器,但我不知道是什么。有什么想法吗?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.company.appname">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:requestLegacyExternalStorage="true">


        <activity
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
            android:name="com.company.appname.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBarLaunch"
            android:launchMode="singleTask">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <provider
            android:name="androidx.core.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"></meta-data>
        </provider>
    </application>

    <!-- Permissions -->

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.CAMERA"/>
</manifest>

【问题讨论】:

【参考方案1】:

所以错误信息实际上是一个神秘的信息。它实际上与 Intents 和 AndroidManifest.xml 无关。没有实际问题是 fileOpener.open() 调用的 MIMEType。传入的类型是 application/octet-stream ,本机设备没有可以打开该类型的应用程序,因此会引发错误。由于此文件是 jpeg,我将其更改为 image/jpeg,并且它打开得很好。这是更新的代码

            this.file.writeFile(this.file.dataDirectory, attachment.name, blob,  replace: true ).then(() => 
              console.log('file should be saved');
              this.file.resolveLocalFilesystemUrl(this.file.dataDirectory + attachment.name).then(fileInfo => 
                const files = fileInfo as FileEntry;
                files.file(success => 
                  this.fileOpener.open(this.file.dataDirectory + attachment.name, success.type).then(() => console.log('file should be open'));
                );
              );
            );

我在 file.resolveLocalFilesystemUrl() 中添加了此调用,因为此调用基本上获取了有关存储文件的元数据。然后您可以获取文件的类型并将其传递给 fileopener.open()

【讨论】:

以上是关于离子角电容器 - 未找到 FileOpener 活动:未找到处理意图的活动的主要内容,如果未能解决你的问题,请参考以下文章

为什么在构造函数中初始化的成员变量会在离子角的ngInit中未定义?

Websocket 离子 3 角 4

text 未找到离子模块'FirebaseInstanceID'

离子角多重嵌套和抽象状态

离子电容器版本号

角/离子结合慢