内容简介:近期混合应用开发需要下载和预览的功能,选择方案为先下载到本地,再使用采用此预览方案文件会被先下载到本地,项目采用
近期混合应用开发需要下载和预览的功能,选择方案为先下载到本地,再使用 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
标签内增加
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
营销三大算法
刘学林、刘逸春、张新春、王颖、余彬晶、刘锦炽、董少灵、沈逸超、王锐睿、孙静若 / 上海交通大学出版社 / 2018-1-31 / 88.00元
未来的营销应该是数字化的,即数字营销。以数据为本,用演算做根,数字营销能够演算生活的方方面面。在数字营销领域,市场的整个投入、产出带来什么东西?企业一定要狠清楚地知道,这是做数字营销的本质。数字营销和企业做生意的本质是一样的,目的都是以投入换取产出。 本书由正和岛数字营销部落编写,基于大量企业的案例与数据,提出了营销三大核心算法与一套全局营销系统,帮助企业CEO与营销人员科学化建立全局营销系......一起来看看 《营销三大算法》 这本书的介绍吧!