简体   繁体   English

使用phonegap将base64字符串保存为图像?

[英]Save base64 string as image using phonegap?

I am getting a base64 string from the server for a png file and i would like to save this image on the file system. 我从服务器获取一个png文件的base64字符串,我想将此图像保存在文件系统上。 It looks like phonegap filewriter only supports binary. 看起来像phonegap文件编写器只支持二进制文件。 Does anyone know how i can convert this base64 string to use in phonegap. 有谁知道如何将这个base64字符串转换为在phonegap中使用。 I was looking at window.atob and window.btoa but couldnt make sense of things. 我在看window.atob和window.btoa,但无法理解事物。

You can not use the PhoneGap FileWriter to write binary data. 您不能使用PhoneGap FileWriter来写二进制数据。 You'd need to write a plugin to send your base64 encoded data to the native side, encode it into binary then write it using native code. 您需要编写一个插件来将base64编码数据发送到本机端,将其编码为二进制文件,然后使用本机代码编写它。

Check out the plugins info at: HERE also take a look at the phonegap source code to see how we do the file writer and you can add some code to do the base64 decode before writing. 查看插件信息: HERE还可以查看phonegap源代码,了解我们如何编写文件编写器,您可以在编写之前添加一些代码来执行base64解码。

As far as I know, phonegap don't provide a way to save an image to the filesystem. 据我所知,phonegap没有提供将图像保存到文件系统的方法。 You need a phonegap plugin to do that. 你需要一个phonegap插件来做到这一点。
Canvas2Image nearly do what you want. Canvas2Image几乎可以做你想要的。 It take a canvas id, extract a base64 string from the canvas and save it as an image. 它采用画布ID,从画布中提取base64字符串并将其另存为图像。 You can slightly alter the javascript part of the plugins to accept a base64 string instead of a canvas id. 您可以稍微更改插件的javascript部分以接受base64字符串而不是画布ID。

你可能想查看这篇文章 ,它将dataUrl是base64保存到android文件系统上的png图像

Just found a good plugin to save BASE64 into filesystem. 刚刚找到一个好的插件将BASE64保存到文件系统中。

Phonegap Image Resizer Phonegap Image Resizer

window.imageResizer.storeImage(
  function(json) { 
    console.log('Saved to ' + json.url) 
  }, function (error) {
    console.log("Error : \r\n" + error);
  }, imageDataInBase64);

More detail see the comment imageresize.js 更多细节请参见注释imageresize.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM