Skip to content

Retrieve the image path also on iOS #68

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
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
<!-- android -->
<platform name="android">

<dependency id="com.android.support:[email protected]" />

<config-file target="AndroidManifest.xml" parent="/*">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</config-file>
Expand Down
42 changes: 38 additions & 4 deletions src/android/Canvas2ImagePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
);
}
}
}
50 changes: 23 additions & 27 deletions src/ios/Canvas2ImagePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Cordova/CDV.h>
#import <AssetsLibrary/AssetsLibrary.h>

@implementation Canvas2ImagePlugin
@synthesize callbackId;
Expand All @@ -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];
}

Expand Down