我有一个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时,我都会收到一个错误,指出Activity Not Found:找不到处理意图的活动。以下是日志
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>发布于 2021-11-10 13:57:49
因此,错误消息实际上是一条隐秘的消息。这实际上与意图和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()
https://stackoverflow.com/questions/69904550
复制相似问题