I am using this phonegap plugin called Screenshot and I am willing to upload the base64 encoded image to facebook. Unfortunately, I get an error from facebook saying that the available formats are only JPG, PNG, GIF and TIFF.
So I either have to find a way to change the java code of the plugin (I have never used java before) or come up with a javascript function in my application which converts it, although I don't really know if that is possible/worth it.
Here is the java code of the screenshot plugin which encodes the image:
else if (action.equals("getScreenshotAsURI")) {
final Integer quality = (Integer) args.get(0);
super.cordova.getActivity().runOnUiThread(new Runnable() {@Override
public void run() {
View view = webView.getRootView();
try {
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
if (bitmap.compress(CompressFormat.JPEG, quality, jpeg_data)) {
byte[] code = jpeg_data.toByteArray();
byte[] output = Base64.encode(code, Base64.NO_WRAP);
String js_out = new String(output);
js_out = "data:image/jpeg;base64," + js_out;
JSONObject jsonRes = new JSONObject();
jsonRes.put("URI", js_out);
PluginResult result = new PluginResult(PluginResult.Status.OK, jsonRes);
callbackContext.sendPluginResult(result);
js_out = null;
output = null;
code = null;
}
jpeg_data = null;
} catch (JSONException e) {
callbackContext.error(e.getMessage());
} catch (Exception e) {
callbackContext.error(e.getMessage());
}
}
});
return true;
}
The entire code of the plugin can be found on the github repository in the beginning of my question. I tried changing CompressForma.JPEG
to CompressForma.JPG
, but had no luck ;(