const saveimgtolocal = (imgdata, filename) => { return new Promise((resolve, reject) => { // 保存到本地 let bitmap = new plus.nativeObj.Bitmap("test"); bitmap.loadBase64Data(imgdata, function() { bitmap.save(filename, {}, function(i) { //console.log('保存图片成功:' + JSON.stringify(i)); bitmap.clear(); resolve("success") }, function(e) { onsole.log('保存图片失败:' + JSON.stringify(e)); bitmap.clear(); reject("error") }); }, function() { console.log('加载Base64图片数据失败:' + JSON.stringify(e)); bitmap.clear(); reject("error") }); }); } var intToByte = (i) => { var b = i & 0xFF; var c = 0; if (b >= 128) { c = b % 128; c = -1 * (128 - c); } else { c = b; } return c } //安卓专用 const savefiletolocal = (filedata, filename) => { return new Promise((resolve, reject) => { // 请求本地系统文件对象 plus.io.PRIVATE_WWW:应用运行资源目录常量 plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fobject) { //fs.root是根目录操作对象DirectoryEntry fobject.root.getFile(filename, { create: true }, function(fileEntry) { // 获得平台绝对路径 var fullPath = fileEntry.fullPath; var FileOutputStream = plus.android.importClass("java.io.FileOutputStream"); //如果文件不存在则创建文件,如果文件存在则删除文件后重新创建文件 var out = new FileOutputStream(fullPath); try { var bytes = []; for (var c = 0; c < filedata.length; c += 2) { bytes.push(intToByte(parseInt(filedata.substr(c, 2), 16))); } console.log('bytes', bytes); out.write(bytes, 0, bytes.length); // byte 数组写入此文件输出流中 //out.writeFile(bytes); // byte 数组写入此文件输出流中。 out.flush(); //刷新写入文件中去。 out.close(); //关闭此文件输出流并释放与此流有关的所有系统资源。 resolve("success") } catch (e) { console.log(e.message); reject(e.message) } }); }); }); } // savefiletolocal(filedata, filename) { // const self = this; // // 请求本地系统文件对象 plus.io.PRIVATE_WWW:应用运行资源目录常量 // plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fobject) { // //fs.root是根目录操作对象DirectoryEntry // fobject.root.getFile(filename, { // create: true // }, function(fileEntry) { // // 获得平台绝对路径 // var fullPath = fileEntry.fullPath; // console.log('平台绝对路径', fullPath); // // 引入安卓原生类 // //var Base64 = plus.android.importClass("android.util.Base64"); // var FileOutputStream = plus.android.importClass("java.io.FileOutputStream"); // //如果文件不存在则创建文件,如果文件存在则删除文件后重新创建文件 // var out = new FileOutputStream(fullPath); // ///var bytes = self.stringToByte(filedata) // try { // var bytes = []; // var len = filedata.length / 2; // for (var i = 0; i < len; i++) { // var cur = i == (len - 1) ? filedata.slice(2 * i) : filedata.slice(2 * i, // 2 * i + 2) // //console.log('cur',cur) // bytes.push(parseInt('0x' + cur, 16)) // } // console.log('bytes', bytes); // out.writeFile(bytes); // byte 数组写入此文件输出流中。 // out.flush(); //刷新写入文件中去。 // out.close(); //关闭此文件输出流并释放与此流有关的所有系统资源。 // //result(fullPath) // } catch (e) { // console.log(e.message); // reject(e.message) // } // // fileEntry.file( function(file){ // // // create a FileWriter to write to the file // // fileEntry.createWriter( function ( writer ) { // // writer.onwrite = function(e){ // // console.log(e); // // console.log(JSON.stringify(e)); // // console.log("文件保存成功"); // // } // // console.log('b棒棒磊') // // writer.seek(0) // // writer.write(filedata); // // }); // // }); // }); // }); // } const loadLocalimg = (filename) => { return new Promise((resolve, reject) => { plus.io.requestFileSystem(plus.io.PRIVATE_WWW, function(fobject) { // fs.root是根目录操作对象DirectoryEntry fobject.root.getFile(filename, { create: false }, function(fileEntry) { fileEntry.file(function(file) { //console.log('file.size',file.size) var fullPath = fileEntry.fullPath; // console.log('平台绝对路径',fullPath); if (file.size == 0) { // self.goods_noimg.push(goodsid) resolve('') } var fileReader = new plus.io.FileReader(); fileReader.readAsDataURL(file, 'utf-8'); fileReader.onloadend = function(evt) { resolve(evt.target.result) //console.log('base:',evt.target.result) //self.resInfo = evt.target.result //self.goodspic = evt.target.result // //赋值到数组中去 // self.allGoods.forEach(item => { // var sid = item.goodsid // if (sid == goodsid) { // //console.log('load local img success:',filename) // item.goodspic = evt.target.result // item.loaded = 'success' // return true; // //item.loaded = 'success' // } // }); } }); }, function(e) { console.log('没有图片:', e) // self.goods_noimg.push(goodsid) resolve('') } ); }); }); } export { saveimgtolocal, savefiletolocal, loadLocalimg }