You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 2 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. const saveimgtolocal = (imgdata, filename) => {
  2. return new Promise((resolve, reject) => {
  3. // 保存到本地
  4. let bitmap = new plus.nativeObj.Bitmap("test");
  5. bitmap.loadBase64Data(imgdata, function() {
  6. bitmap.save(filename, {}, function(i) {
  7. //console.log('保存图片成功:' + JSON.stringify(i));
  8. bitmap.clear();
  9. resolve("success")
  10. }, function(e) {
  11. onsole.log('保存图片失败:' + JSON.stringify(e));
  12. bitmap.clear();
  13. reject("error")
  14. });
  15. }, function() {
  16. console.log('加载Base64图片数据失败:' + JSON.stringify(e));
  17. bitmap.clear();
  18. reject("error")
  19. });
  20. });
  21. }
  22. var intToByte = (i) => {
  23. var b = i & 0xFF;
  24. var c = 0;
  25. if (b >= 128) {
  26. c = b % 128;
  27. c = -1 * (128 - c);
  28. } else {
  29. c = b;
  30. }
  31. return c
  32. }
  33. //安卓专用
  34. const savefiletolocal = (filedata, filename) => {
  35. return new Promise((resolve, reject) => {
  36. // 请求本地系统文件对象 plus.io.PRIVATE_WWW:应用运行资源目录常量
  37. plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fobject) {
  38. //fs.root是根目录操作对象DirectoryEntry
  39. fobject.root.getFile(filename, {
  40. create: true
  41. }, function(fileEntry) {
  42. // 获得平台绝对路径
  43. var fullPath = fileEntry.fullPath;
  44. var FileOutputStream = plus.android.importClass("java.io.FileOutputStream");
  45. //如果文件不存在则创建文件,如果文件存在则删除文件后重新创建文件
  46. var out = new FileOutputStream(fullPath);
  47. try {
  48. var bytes = [];
  49. for (var c = 0; c < filedata.length; c += 2) {
  50. bytes.push(intToByte(parseInt(filedata.substr(c, 2), 16)));
  51. }
  52. console.log('bytes', bytes);
  53. out.write(bytes, 0, bytes.length); // byte 数组写入此文件输出流中
  54. //out.writeFile(bytes); // byte 数组写入此文件输出流中。
  55. out.flush(); //刷新写入文件中去。
  56. out.close(); //关闭此文件输出流并释放与此流有关的所有系统资源。
  57. resolve("success")
  58. } catch (e) {
  59. console.log(e.message);
  60. reject(e.message)
  61. }
  62. });
  63. });
  64. });
  65. }
  66. // savefiletolocal(filedata, filename) {
  67. // const self = this;
  68. // // 请求本地系统文件对象 plus.io.PRIVATE_WWW:应用运行资源目录常量
  69. // plus.io.requestFileSystem(plus.io.PRIVATE_DOC, function(fobject) {
  70. // //fs.root是根目录操作对象DirectoryEntry
  71. // fobject.root.getFile(filename, {
  72. // create: true
  73. // }, function(fileEntry) {
  74. // // 获得平台绝对路径
  75. // var fullPath = fileEntry.fullPath;
  76. // console.log('平台绝对路径', fullPath);
  77. // // 引入安卓原生类
  78. // //var Base64 = plus.android.importClass("android.util.Base64");
  79. // var FileOutputStream = plus.android.importClass("java.io.FileOutputStream");
  80. // //如果文件不存在则创建文件,如果文件存在则删除文件后重新创建文件
  81. // var out = new FileOutputStream(fullPath);
  82. // ///var bytes = self.stringToByte(filedata)
  83. // try {
  84. // var bytes = [];
  85. // var len = filedata.length / 2;
  86. // for (var i = 0; i < len; i++) {
  87. // var cur = i == (len - 1) ? filedata.slice(2 * i) : filedata.slice(2 * i,
  88. // 2 * i + 2)
  89. // //console.log('cur',cur)
  90. // bytes.push(parseInt('0x' + cur, 16))
  91. // }
  92. // console.log('bytes', bytes);
  93. // out.writeFile(bytes); // byte 数组写入此文件输出流中。
  94. // out.flush(); //刷新写入文件中去。
  95. // out.close(); //关闭此文件输出流并释放与此流有关的所有系统资源。
  96. // //result(fullPath)
  97. // } catch (e) {
  98. // console.log(e.message);
  99. // reject(e.message)
  100. // }
  101. // // fileEntry.file( function(file){
  102. // // // create a FileWriter to write to the file
  103. // // fileEntry.createWriter( function ( writer ) {
  104. // // writer.onwrite = function(e){
  105. // // console.log(e);
  106. // // console.log(JSON.stringify(e));
  107. // // console.log("文件保存成功");
  108. // // }
  109. // // console.log('b棒棒磊')
  110. // // writer.seek(0)
  111. // // writer.write(filedata);
  112. // // });
  113. // // });
  114. // });
  115. // });
  116. // }
  117. const loadLocalimg = (filename) => {
  118. return new Promise((resolve, reject) => {
  119. plus.io.requestFileSystem(plus.io.PRIVATE_WWW, function(fobject) {
  120. // fs.root是根目录操作对象DirectoryEntry
  121. fobject.root.getFile(filename, {
  122. create: false
  123. }, function(fileEntry) {
  124. fileEntry.file(function(file) {
  125. //console.log('file.size',file.size)
  126. var fullPath = fileEntry.fullPath;
  127. // console.log('平台绝对路径',fullPath);
  128. if (file.size == 0) {
  129. // self.goods_noimg.push(goodsid)
  130. resolve('')
  131. }
  132. var fileReader = new plus.io.FileReader();
  133. fileReader.readAsDataURL(file, 'utf-8');
  134. fileReader.onloadend = function(evt) {
  135. resolve(evt.target.result)
  136. //console.log('base:',evt.target.result)
  137. //self.resInfo = evt.target.result
  138. //self.goodspic = evt.target.result
  139. // //赋值到数组中去
  140. // self.allGoods.forEach(item => {
  141. // var sid = item.goodsid
  142. // if (sid == goodsid) {
  143. // //console.log('load local img success:',filename)
  144. // item.goodspic = evt.target.result
  145. // item.loaded = 'success'
  146. // return true;
  147. // //item.loaded = 'success'
  148. // }
  149. // });
  150. }
  151. });
  152. },
  153. function(e) {
  154. console.log('没有图片:', e)
  155. // self.goods_noimg.push(goodsid)
  156. resolve('')
  157. }
  158. );
  159. });
  160. });
  161. }
  162. export {
  163. saveimgtolocal,
  164. savefiletolocal,
  165. loadLocalimg
  166. }