I am writing a phonegap application for Android and at some point I save the PNG base64 string as a file. However, I noticed that the line is simply flushed to the file and cannot be considered as an image when opened.
I would like to be able to save an image generated from a base64 string. This is what I have:
Javascript (created for a phone call):
var dataURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
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);
}
function fail(error) {
console.log(error.code);
}
I tried editing the code to save only image data, but it didn't work either. (See below)
function gotFileWriter(writer) {
var justTheData = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
writer.write(justTheData);
}
HTML that runs everything:
<p onclick="window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail)">Save a Dot</p>
Please, help. Thank.
source
share