diff --git a/plugin.xml b/plugin.xml index a05a8fa..fe64d33 100644 --- a/plugin.xml +++ b/plugin.xml @@ -37,6 +37,8 @@ + + diff --git a/src/android/Canvas2ImagePlugin.java b/src/android/Canvas2ImagePlugin.java index ac3f1ba..5a2c198 100644 --- a/src/android/Canvas2ImagePlugin.java +++ b/src/android/Canvas2ImagePlugin.java @@ -10,12 +10,16 @@ import org.json.JSONArray; import org.json.JSONException; +import android.Manifest; +import android.app.Activity; import android.content.Intent; +import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.Environment; +import android.support.v4.app.ActivityCompat; import android.util.Base64; import android.util.Log; @@ -48,7 +52,7 @@ public boolean execute(String action, JSONArray data, if (bmp == null) { callbackContext.error("The image could not be decoded"); } else { - + // Save the image File imageFile = savePhoto(bmp); if (imageFile == null) @@ -70,7 +74,9 @@ private File savePhoto(Bitmap bmp) { File retVal = null; try { - Calendar c = Calendar.getInstance(); + verifyStoragePermissions(this.cordova.getActivity()); + + Calendar c = Calendar.getInstance(); String date = "" + c.get(Calendar.DAY_OF_MONTH) + c.get(Calendar.MONTH) + c.get(Calendar.YEAR) @@ -122,5 +128,33 @@ private void scanPhoto(File imageFile) Uri contentUri = Uri.fromFile(imageFile); mediaScanIntent.setData(contentUri); cordova.getActivity().sendBroadcast(mediaScanIntent); - } -} + } + + // Storage Permissions + private static final int REQUEST_EXTERNAL_STORAGE = 1; + private static String[] PERMISSIONS_STORAGE = { + Manifest.permission.READ_EXTERNAL_STORAGE, + Manifest.permission.WRITE_EXTERNAL_STORAGE + }; + + /** + * Checks if the app has permission to write to device storage + * + * If the app does not has permission then the user will be prompted to grant permissions + * + * @param activity + */ + public static void verifyStoragePermissions(Activity activity) { + // Check if we have write permission + int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); + + if (permission != PackageManager.PERMISSION_GRANTED) { + // We don't have permission so prompt the user + ActivityCompat.requestPermissions( + activity, + PERMISSIONS_STORAGE, + REQUEST_EXTERNAL_STORAGE + ); + } + } +} \ No newline at end of file diff --git a/src/ios/Canvas2ImagePlugin.m b/src/ios/Canvas2ImagePlugin.m index ad063ae..44b418e 100644 --- a/src/ios/Canvas2ImagePlugin.m +++ b/src/ios/Canvas2ImagePlugin.m @@ -4,11 +4,12 @@ // // Created by Tommy-Carlos Williams on 29/03/12. // Copyright (c) 2012 Tommy-Carlos Williams. All rights reserved. -// MIT Licensed +// MIT Licensed // #import "Canvas2ImagePlugin.h" #import +#import @implementation Canvas2ImagePlugin @synthesize callbackId; @@ -22,35 +23,30 @@ @implementation Canvas2ImagePlugin - (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command { self.callbackId = command.callbackId; - 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)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.commandDelegate sendPluginResult:result callbackId:self.callbackId]; - } - else // No errors - { - // Show message image successfully saved - NSLog(@"IMAGE SAVED!"); - CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:@"Image saved"]; - [self.commandDelegate sendPluginResult:result callbackId:self.callbackId]; - } + NSData* imageData = [[NSData alloc] initWithBase64EncodedString:[command.arguments objectAtIndex:0] options:0]; + + UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease]; + ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; + // Request to save the image to camera roll + [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:ALAssetOrientationRight /*(ALAssetOrientation)[image imageOrientation]*/ completionBlock:^(NSURL *assetURL, NSError *error){ + if (error) { + // Show error message... + NSLog(@"ERROR: %@",error); + CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description]; + [self.commandDelegate sendPluginResult:result callbackId:self.callbackId]; + } else { + // Show message image successfully saved + NSLog(@"IMAGE SAVED!"); + CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString: [assetURL absoluteString]]; + [self.commandDelegate sendPluginResult:result callbackId:self.callbackId]; + } + }]; + [library release]; } - (void)dealloc -{ - [callbackId release]; +{ + [callbackId release]; [super dealloc]; }