Upgrading PhoneGap Plugin for Saving Images

If you missed my initial post about Saving Images from PhoneGap then you can go check it out (it is not very long). After I created the plugin however, I was getting warnings in eclipse that the Plugin library had been depreciated. While it still worked, this obviously means that it will eventually go away. Being the bleeding edge of technology like I like to be, I wanted to find a way to upgrade it to what is current. Here’s how I did it:

  1. First of all make sure you check out the original code for reference.
  2. Next the only class/file that you will need to edit is the Base64ToPNG.java.
  3. Add the following imports:
  4. import org.apache.cordova.api.CallbackContext;
    import org.apache.cordova.api.CordovaPlugin;
  5. Remove these imports:
  6. import org.apache.cordova.api.Plugin;
    import org.apache.cordova.api.PluginResult;
  7. Change what the class extends from Plugin to Cordova Plugin:
  8. public class Base64ToPNG extends CordovaPlugin {
  9. Now, change the overwritten execute method to return a boolean instead of PluginResult and the string to a CallbackContext from a string:
  10. public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
  11. Anywhere that returns a PluginResult will need to be changed to return true or false depending on the specific action previously taken. You will also want to assign error message and success messages. Here is the specific success message:
  12. callbackContext.success("Saved successfully!");
    return true;

The end result of what the class now looks like:

package org.apache.cordova;

* A phonegap plugin that converts a Base64 String to a PNG file.
* @author mcaesar
* @lincese MIT.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;

import android.os.Environment;

import java.io.*;

import org.json.JSONException;
import org.json.JSONObject;
import util.Base64;

public class Base64ToPNG extends CordovaPlugin {

public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
if (!action.equals("saveImage")) {
return false;
try {
String b64String = args.getString(0);
            if (b64String.startsWith("data:image")) {
b64String = b64String.substring(b64String.indexOf(',') + 1);
JSONObject params = args.getJSONObject(1);

//Optional parameter
String filename = params.has("filename")
? params.getString("filename")
: "drawsaur_" + System.currentTimeMillis() + ".png";

String folder = params.has("folder")
? params.getString("folder")
: Environment.getExternalStorageDirectory() + "/drawsaur";

Boolean overwrite = params.has("overwrite")
? params.getBoolean("overwrite")
: false;

return this.saveImage(b64String, filename, folder, overwrite, callbackContext);

} catch (JSONException e) {
return false;
} catch (InterruptedException e) {
return false;
private boolean saveImage(String b64String, String fileName, String dirName, Boolean overwrite, CallbackContext callbackContext) throws InterruptedException, JSONException {
try {
//Directory and File
File dir = new File(dirName);
if (!dir.exists()) {
File file = new File(dirName, fileName);
//Avoid overwriting a file
if (!overwrite && file.exists()) {
callbackContext.error("File already exists!");
return true;
//Decode Base64 back to Binary format
byte[] decodedBytes = Base64.decode(b64String.getBytes());
//Save Binary file to phone
FileOutputStream fOut = new FileOutputStream(file);

callbackContext.success("Saved successfully to " + dirName + "/" + fileName);
return true;
} catch (FileNotFoundException e) {
callbackContext.error("File not Found!");
return false;
} catch (IOException e) {
return false;

Update 7/8/13:
Fixed the code so that it parses the b64String properly.

Tagged with:

1 thought on “Upgrading PhoneGap Plugin for Saving Images”

  • String b64String = "";
    if (b64String.startsWith("data:image")) {
    b64String = args.getString(0).substring(21);
    } else {
    b64String = args.getString(0);

    should be:

    String b64String = args.getString(0);
    if (b64String.startsWith("data:image")) {
    b64String = b64String.substring(
    b64String.indexOf(',') + 1

Comments are closed.