title: Phonegap - How to generate image file from base64 string?
body: <p>Am writing a phonegap app for Android and at one point, I save a base64 PNG string as a file. However, I have observed that the string is just dumped into a file and can't be viewed as an image when opened. </p>
  
  <p>I'd like to be able to save an image generated from the base64 string.This is what I have:</p>
  
  <p><strong>The Javascript (Formated for Phonegap):</strong></p>
  
  <pre><code>/*** 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);
  }
  </code></pre>
  
  <p>I tried editing the code to save just the image data but it did't work either. (See below)</p>
  
  <pre><code>function gotFileWriter(writer) {
       var justTheData = dataURL.replace(/^data:image\/(png|jpg);base64,/, "");//Removes everything up to ...'base64,'
       writer.write(justTheData); //also does not show
   }
  </code></pre>
  
  <p><strong>The HTML that triggers everthing:</strong></p>
  
  <pre><code>&lt;p onclick="window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail)"&gt;Save a Dot&lt;/p&gt;
  </code></pre>
  
  <p>Please help. Thank you.</p>