How to save an imageSource object to the device's Photo Library on both iOS and Android


There’s not a built in way to save an Image to a device’s photo library in Nativescript, so you have to dig a little bit into the native APIs. I had done this for iOS a while ago and just figured out how to do it in Android so I figured I’d share it! The code is below. The function accepts an image-source object. In my case I got it from doing

imageSourceModule.fromUrl(url).then(function(imageSource) {
  photosModel.saveToAlbum(imageSource, false, false, function() {
     alert('The photo was saved!');

In my app theres a view where you can see a photo gallery of images that you may not have uploaded yourself (sort of like a shared photo stream), and theres a button you can press to save the image to your photo library.

Here’s the function:

viewModel.saveToAlbum = function(imageSource, format, quality, callBack) {
    if (app.ios) {
        var res = false;
        if (!imageSource) {
            return res;
        var result = true;
        var CompletionTarget = NSObject.extend({
            "thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:": function(
                image, error, context) {
                if (error) {
                    result = false;
        }, {
            exposedMethods: {
                "thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:": {
                    returns: interop.types.void,
                    params: [UIImage, NSError, interop.Pointer]
        var completionTarget =;
        UIImageWriteToSavedPhotosAlbum(imageSource.ios, completionTarget,
            if (callBack) callBack();
        return result;
    } else {
        function broadCast(imageFile) {

            var mediaScanIntent = new android.content.Intent(android.content.Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            var contentUri =;
            if (callBack) callBack();
            return result;

        var folderPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_PICTURES).toString();
        var fileName = 'img_' + new Date().getTime() + '.jpg';
        var path = fsModule.path.join(folderPath, fileName);
        var exists = fsModule.File.exists(path);
        if (!exists) {
            var saved = imageSource.saveToFile(path, enums.ImageFormat.jpeg);
            if (saved) broadCast(new;



Can u pls share full source code of saving image to photo album.