Cordova 11
android studio 2022.2.1
在使用 cordova-plugin-file-transfer 下载服务器的apk 然后使用 cordova-plugin-file-opener2 安装apk时 报错!
fileOpener2.open "Failed to find configured root that contains /data/data/com.sjdt.mobile/files/android.apk"
"cordova-plugin-file-transfer": "2.0.0-dev",
"cordova-plugin-file": "7.0.0", "cordova-plugin-file-opener2": "4.0.0",
debugger;
var self=this;
var downloadURl =this.appInfo[this.env].downloadURL;
var targetPath =cordova.file.externalDataDirectory;
if (Browser.isX5App) {
if (Browser.isAndroid) {
var fileTransfer = new window.FileTransfer();
var uri = encodeURI(downloadURl);
window.plugins.spinnerDialog.show("资源更新", "正在下载apk,请稍后...", true);
var fileURL =targetPath+ 'android.apk'; //apk保存的目录
//cdvfile://localhost/temporary/path/to/file.mp3
fileTransfer.download(uri, fileURL, function (entry) {
window.plugins.spinnerDialog.hide();
window.plugins.spinnerDialog.show("开始安装", "下载成功,准备安装!");
console.log("apk-url--------"+entry.nativeURL);
cordova.plugins.fileOpener2.open( entry.nativeURL, 'application/vnd.android.package-archive', {
error: function(msg) {
window.plugins.spinnerDialog.hide();
console.log('安装文件打开失败'+msg);
},
success: function() {
window.plugins.spinnerDialog.hide();
console.log('安装文件打开成功');
}
})
},
function (error) {
alert('download error: ' + error.source + error.target + error.code);
window.plugins.spinnerDialog.hide();
},
false, {headers: {'Authorization': 'Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=='}}
);
}
}else{
window.open(downloadURl, '_system');
}
//使用此路径targetPath
var targetPath =cordova.file.externalDataDirectory;
需要修改 需要在此目录下增加 android\app\src\main\res\xml\opener_paths.xml 文件中的最后 新增 <root-path name="root_path" path="."/> 否则报错
fileOpener2.open "Failed to find configured root that contains /data/data/com.sjdt.mobile/files/android.apk"
这个问题不知道啥。。。。
//"cdvfile://localhost/temporary/path/to/"; 使用此路径 提示File not Found
另外网上说的 这些我都加上了 在config.xml 中
<platform name="android">
<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>
</platform>
<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external,root" />