50 lines
1.6 KiB
JavaScript
50 lines
1.6 KiB
JavaScript
export default function lzFileWriter(id, fn) {
|
||
|
||
plus.io.resolveLocalFileSystemURL('/static/html/index.html', function(entry) {
|
||
let _we = plus.webview.create(entry.toLocalURL());
|
||
_we.evalJS(`settitle("${id}");`)
|
||
plus.globalEvent.addEventListener('plusMessage', (e) => {
|
||
if(e.data.id==id){
|
||
geturl(e.data.img, e.data.id + ".png",e.data.id)
|
||
_we.close()
|
||
}
|
||
|
||
})
|
||
})
|
||
|
||
function geturl(base64, fileName,imgid) {
|
||
plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fs) {
|
||
fs.root.getFile(fileName, {
|
||
create: true
|
||
}, function(fileEntry) {
|
||
// 获得平台绝对路径
|
||
var fullPath = fileEntry.fullPath;
|
||
|
||
// 引入安卓原生类
|
||
var Base64 = plus.android.importClass("android.util.Base64");
|
||
var FileOutputStream = plus.android.importClass("java.io.FileOutputStream");
|
||
//如果文件不存在则创建文件,如果文件存在则删除文件后重新创建文件
|
||
var out = new FileOutputStream(fullPath);
|
||
/**
|
||
* 此处需要把base64前缀去除,在写入字节流数组
|
||
* 去除头部data:image/jpg;base64,留下base64编码后的字符串
|
||
**/
|
||
let index = base64.indexOf(',')
|
||
let base64Str = base64.slice(index + 1, base64.length)
|
||
//base64解密得到字节流bytes;
|
||
var bytes = Base64.decode(base64Str, 0);
|
||
try {
|
||
out.write(bytes); // byte 数组写入此文件输出流中。
|
||
out.flush(); //刷新写入文件中去。
|
||
out.close(); //关闭此文件输出流并释放与此流有关的所有系统资源。
|
||
uni.setStorageSync(imgid,fullPath)
|
||
fn(fullPath)
|
||
} catch (e) {
|
||
console.log(e.message);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
|
||
} |