Mitch D Mitch D - 1 year ago 77
Android Question

Save Downloaded Image to Gallery Android

I'm making a cordova plugin that takes a remote image, downloads it using cordova FileTransfer plugin, and then saves it to the gallery in Android or Camera Roll in iOS. I have the image path and was able to figure it out for iOS but I can't seem to get it for Android. Here is my working code for iOS:

NSString* ImagePath = [[command arguments] objectAtIndex:0]; //path to downloaded image
UIImage* image = [UIImage imageWithContentsOfFile:ImagePath];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

For android it seems like the function I want to use is addImageToMediaGallery, which I am attempting to call like so:

String path = data.getString(0);
$product->addImageToMediaGallery(path, null, false, false);

However, Android Studio returns the error "Cannot resolve method addImageToMediaGallery(java.lang.string,null,boolean,boolean)"

Thanks in advance for any help

Answer Source

I was unable to get this to work, so I instead used a different function I found from

String path = data.getString(0);
Activity ctx = this.cordova.getActivity();

try {
    MediaStore.Images.Media.insertImage(ctx.getContentResolver(), path, "" , "");
} catch (FileNotFoundException e) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download