内容简介:近期混合应用开发需要下载和预览的功能,选择方案为先下载到本地,再使用采用此预览方案文件会被先下载到本地,项目采用
近期混合应用开发需要下载和预览的功能,选择方案为先下载到本地,再使用 cordova-plugin-file-opener2
插件进行预览。
采用此预览方案文件会被先下载到本地, cordova-plugin-file-opener2
插件其实可以直接打开网络地址来实现预览,采用此方式是基于以下考虑:
-
避免重复下载(因app中还有下载功能)
-
避免有文件格式解析错误的情况,用户可以到本地再次进行查看
-
下载目录可控
2.框架
项目采用 Cordova + Vue + MintUI
3.操作
-
根据不同系统,选择不同的方法创建目录。
-
Android :
-
window.resolveLocalFileSystemURL可访问沙盒存储之外的文件系统位置 -
cordova.file.externalRootDirectory外部储存(sd卡)
-
-
IOS :
window.requestFileSystem
-
getEntry() {
let _this = this
let platform = device.platform.toLowerCase() // cordova-plugin-device 获取系统
if (platform === "android") {
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, _this.onFileSystemSuccess, _this.onError);
} else {
// for iOS
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, _this.onFileSystemSuccess, _this.onError);
}
},
复制代码
-
onFileSystemSuccess成功回调接收fileSystem对象-
fileSystem.root返回一个DirectoryEntry对象,你可以用它来创建或获取文件(通过调用getFile) -
entry.getDirectory()创建目录
-
onFileSystemSuccess(fileSystem) {
let _this = this
let entry = "";
let platform = device.platform.toLowerCase()
if (platform === "android") {
entry = fileSystem;
} else {
entry = fileSystem.root;
}
entry.getDirectory("Cordova", {
create: true,
exclusive: false
}, _this.onGetDirectorySuccess, _this.onGetDirectoryFail);
},
复制代码
-
成功创建目录
onGetDirectorySuccess接收DirectoryEntry对象-
dir.getFile创建文件
-
onGetDirectorySuccess(dir) {
let _this = this
this.cdr = dir;
dir.getFile(_this.fileName, {
create: true,
exclusive: false
}, _this.downloadFile, _this.errorHandler);
},
复制代码
-
成功创建文件,通过下载写入文件
-
使用
cordova-plugin-file-transfer下载并写入文件
-
使用
downloadFile(fileEntry) {
this.$toast('正在下载...');
let fileTransfer = new FileTransfer();
let _this = this
fileTransfer.download(
encodeURI(_this.savePath), //uri网络下载路径
fileEntry.toURL(), //文件本地存储路径
function (entry) {
_this.$toast('下载成功');
// 下载完成执行本地预览
_this.preView(fileEntry);
},
function (error) {
_this.$toast('下载失败');
},
false, {
headers: {
'Authorization': `Bearer ${localStorage.getItem('CFA0')}`
},
}
);
}
复制代码
-
下载完成,预览文件
-
使用
cordova-plugin-file-opener2打开文件 -
mineType使用mime-types获取,提供扩展名即可获取。
-
preView(fileEntry) {
let _this = this;
cordova.plugins.fileOpener2.showOpenWithDialog(
// 此处必须填写cdvfile://地址,不然android7.0+会报文件权限错误
fileEntry.toInternalURL(), //文件本地地址转cdvfile://地址
_this.mineType, //文件类型
{
error: function (e) {
console.log(e, 'Error status: ' + e.status + ' - Error message: ' + e.message);
},
success: function () {
_this.$toast('开始预览');
}
},
);
},
复制代码
4. 可能遇到的坑
在预览文件的时候 cordova-plugin-file-opener2
有可能会报以下错误:
Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
寻找很久,应该是因为权限问题导致,解决办法如下:
在 AndroidManifest.xml
中 application
标签内增加
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
网络多人游戏架构与编程
格雷泽 (Joshua Glazer)、马达夫 (Sanjay Madhav) / 王晓慧、张国鑫 / 人民邮电出版社 / 2017-10-1 / CNY 109.00
本书是一本深入探讨关于网络多人游戏编程的图书。 全书分为13章,从网络游戏的基本概念、互联网、伯克利套接字、对象序列化、对象复制、网络拓扑和游戏案例、延迟、抖动和可靠性、改进的延迟处理、可扩展性、安全性、真实世界的引擎、玩家服务、云托管专用服务器等方面深入介绍了网络多人游戏开发的知识,既全面又详尽地剖析了众多核心概念。 本书的多数示例基于C++编写,适合对C++有一定了解的读者阅读。本......一起来看看 《网络多人游戏架构与编程》 这本书的介绍吧!