Skip to content
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
Added support for JPEG Base64 images URIs passed as file name.

Example
======


```

var nativePathToJpegImage = 'data:image/jpeg;base64,<data>'

window.cordova.plugins.imagesaver.saveImageToGallery(nativePathToJpegImage, onSaveImageSuccess, onSaveImageError);

```


SaveImage
======

Expand Down
27 changes: 27 additions & 0 deletions src/android/SaveImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;

/**
Expand Down Expand Up @@ -77,6 +78,31 @@ private void saveImageToGallery(JSONArray args, CallbackContext callback) throws
*/
private void performImageSave() throws JSONException {
// create file from passed path

// BoB0: patch per supportare i file base64
String ext = "";
String dataB64 = "";
if (filePath.indexOf("data:image/jpeg;base64,") == 0) {
ext = "jpg";
dataB64 = filePath.substring("data:image/jpeg;base64,".length());
}

if (!ext.equals("")) {
try {
byte[] data = Base64.decode(dataB64, Base64.DEFAULT);
File outputFile = File.createTempFile("image", ext);
FileOutputStream stream = new FileOutputStream(outputFile);
stream.write(data);
stream.close();
filePath = outputFile.getAbsolutePath();

} catch (Exception e) {
callbackContext.error("RuntimeException occurred: " + e.getMessage());
}

}


File srcFile = new File(filePath);

// destination gallery folder - external storage
Expand Down Expand Up @@ -191,3 +217,4 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int
}
}
}

13 changes: 12 additions & 1 deletion src/ios/SaveImage.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,21 @@ - (void)saveImageToGallery:(CDVInvokedUrlCommand*)command {

NSLog(@"Image absolute path: %@", imgAbsolutePath);

UIImage *image = [UIImage imageWithContentsOfFile:imgAbsolutePath];
UIImage *image = nil;
if ([imgAbsolutePath hasPrefix:@"data:image/jpeg;base64,"]) {
image = [self decodeBase64ToImage: [imgAbsolutePath substringFromIndex:[@"data:image/jpeg;base64," length]]];
} else {
image = [UIImage imageWithContentsOfFile:imgAbsolutePath];
}
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}];
}

- (UIImage *)decodeBase64ToImage:(NSString *)strEncodeData {
NSData *data = [[NSData alloc]initWithBase64EncodedString:strEncodeData options:NSDataBase64DecodingIgnoreUnknownCharacters];
return [UIImage imageWithData:data];
}

- (void)dealloc {
[callbackId release];
[super dealloc];
Expand All @@ -39,3 +49,4 @@ - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error context
}

@end