[英]Phonegap - How to generate image file from base64 string?
Am writing a phonegap app for Android and at one point, I save a base64 PNG string as a file. 我正在为Android编写一个phonegap应用程序,我将base64 PNG字符串保存为文件。 However, I have observed that the string is just dumped into a file and can't be viewed as an image when opened. 但是,我发现字符串只是转储到文件中,打开时无法将其视为图像。
I'd like to be able to save an image generated from the base64 string.This is what I have: 我希望能够保存从base64字符串生成的图像。这就是我所拥有的:
The Javascript (Formated for Phonegap): Javascript(Formated for Phonegap):
/*** Saving The Pic ***/
var dataURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; //A simple red dot
function gotFS(fileSystem) {
fileSystem.root.getFile("dot.png", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.write(dataURL); //does not open as image
}
function fail(error) {
console.log(error.code);
}
I tried editing the code to save just the image data but it did't work either. 我尝试编辑代码只保存图像数据,但它也没有用。 (See below) (见下文)
function gotFileWriter(writer) {
var justTheData = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");//Removes everything up to ...'base64,'
writer.write(justTheData); //also does not show
}
The HTML that triggers everthing: 触发everthing的HTML:
<p onclick="window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail)">Save a Dot</p>
Please help. 请帮忙。 Thank you. 谢谢。
You just need to set the src of your image to the base 64 data to view the image. 您只需将图像的src设置为基本64数据即可查看图像。
var image = document.getElementById('myImage');
image.src = "data:image/jpeg;base64," + justTheData;
here no need to replace base64 string, just set convert string by following: 这里不需要替换base64字符串,只需按以下方式设置转换字符串:
in javascript: 在javascript中:
var image = document.getElementById('myImage');
image.src = justTheData;// this convert data
in jquery: 在jquery中:
$("#imageid").attr("src",justTheData)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.