Skip to content

Android Improvements. Make call async. Use streams instead of buffers… #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 44 additions & 24 deletions src/android/Canvas2ImagePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import android.util.Base64;
import android.util.Log;


import java.io.StringBufferInputStream;
import android.util.Base64InputStream;

/**
* Canvas2ImagePlugin.java
*
Expand All @@ -32,33 +36,49 @@ public class Canvas2ImagePlugin extends CordovaPlugin {
public static final String ACTION = "saveImageDataToLibrary";

@Override
public boolean execute(String action, JSONArray data,
CallbackContext callbackContext) throws JSONException {
public boolean execute(final String action, final JSONArray data,
final CallbackContext callbackContext) throws JSONException {

if (action.equals(ACTION)) {

String base64 = data.optString(0);
if (base64.equals("")) // isEmpty() requires API level 9
callbackContext.error("Missing base64 string");

// Create the bitmap from the base64 string
Log.d("Canvas2ImagePlugin", base64);
byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
if (bmp == null) {
callbackContext.error("The image could not be decoded");
} else {

// Save the image
File imageFile = savePhoto(bmp);
if (imageFile == null)
callbackContext.error("Error while saving image");

// Update image gallery
scanPhoto(imageFile);

callbackContext.success(imageFile.toString());
}
cordova.getThreadPool().execute(new Runnable() {
public void run() {

StringBufferInputStream inStream = null;
// Anonymous block to allow GC collection asap if necessary.
{
String base64 = data.optString(0);
if (base64.equals("")) {
// isEmpty() requires API level 9
callbackContext.error("Missing base64 string");
return;
}
inStream = new StringBufferInputStream(base64);
}

Base64InputStream b64in = new Base64InputStream(inStream, 0);

// Create the bitmap from the base64 string
//Log.d("Canvas2ImagePlugin ", Integer.toString(base64.length()));
//byte[] decodedString = Base64.decode(base64, Base64.DEFAULT);
//Bitmap bmp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Bitmap bmp = BitmapFactory.decodeStream(b64in);
if (bmp == null) {
callbackContext.error("The image could not be decoded");
} else {

// Save the image
File imageFile = savePhoto(bmp);
if (imageFile == null)
callbackContext.error("Error while saving image");

// Update image gallery
scanPhoto(imageFile);

callbackContext.success(imageFile.toString());
}
}
});

return true;
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/ios/Canvas2ImagePlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@

- (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command;

- (void)saveImageBinaryDataToLibrary:(CDVInvokedUrlCommand*)command;

@end
35 changes: 20 additions & 15 deletions src/ios/Canvas2ImagePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,49 @@
@implementation Canvas2ImagePlugin
@synthesize callbackId;

//-(CDVPlugin*) initWithWebView:(UIWebView*)theWebView
//{
// self = (Canvas2ImagePlugin*)[super initWithWebView:theWebView];
// return self;
//}

- (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command
{
self.callbackId = command.callbackId;
NSData* imageData = [NSData dataFromBase64String:[command.arguments objectAtIndex:0]];

UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
NSData* imageData = [[NSData alloc] initWithBase64EncodedString:[command.arguments objectAtIndex:0] options:0];


UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}

- (void)saveImageBinaryDataToLibrary:(CDVInvokedUrlCommand*)command
{
self.callbackId = command.callbackId;
NSData* imageData = [[command.arguments objectAtIndex:0] dataUsingEncoding:NSUTF8StringEncoding];

UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
// Was there an error?
if (error != NULL)
{
// Show error message...
NSLog(@"ERROR: %@",error);
CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description];
[self.webView stringByEvaluatingJavaScriptFromString:[result toErrorCallbackString: self.callbackId]];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
else // No errors
{
// Show message image successfully saved
NSLog(@"IMAGE SAVED!");
CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:@"Image saved"];
[self.webView stringByEvaluatingJavaScriptFromString:[result toSuccessCallbackString: self.callbackId]];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:pluginResult callbackId:self.callbackId];
}
}

- (void)dealloc
{
[callbackId release];
[callbackId release];
[super dealloc];
}

Expand Down