我尝试从ionic4的https://github.com/pwlin/cordova-plugin-file-opener2的“快速示例”中进行“打开安装对话框”
这是我的代码:
进口:
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer/ngx';
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { File } from '@ionic-native/file/ngx';
下载并打开文件:
const fileName = 'last.apk';
const fileMime = 'application/vnd.android.package-archive';
//const fileName = 'last.pdf';
//const fileMime = 'application/pdf';
const downloadLink = me.settings.api.bin + fileName;
const basePath = me.file.externalDataDirectory; //dataDirectory, externalApplicationStorageDirectory
const downloadPath = 'apk';
me.file.createDir(basePath, downloadPath, true).then(function(link) {
const savePath = basePath + downloadPath + '/' + fileName;
const fileTransfer: FileTransferObject = me.transfer.create();
fileTransfer.download(downloadLink, savePath).then((entry) => {
me.fileOpener.open(entry.toURL(), fileMime)
.then(() => console.log('File is opened'))
.catch(e => console.log('Error opening file', e));
}, (err) => {
});
});
工作正常,PDF已下载并打开。
APK失败,文件下载成功,并且文件打开器显示“文件已打开”,但窗口未出现安装对话框,这是什么问题?
答案 0 :(得分:0)
我找到了解决方案,将此行添加到config.xml
<config-file parent="/manifest" target="AndroidManifest.xml" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
</config-file>