Search code examples
angularcordovacordova-pluginsionic-nativecordova-plugin-camera

saveToPhotoAlbum completely ignored on both Android and iOS


In my ionic 4 app, camera is working fine taking pictures and uploading to a remote server. I use the plugin also to get images already in the gallery on another page, and it works too. The only thing I can't manage to solve is that pictures that are taken while using the app with the camera plugin aren't saved in the Photo Reel or in any other album, despite the fact that saveToPhotoAlbum is set to true

This is the part of the code with the cameraOptions. I use different DestinationTypes based on the platform

        var uri_type;
        if (this.platform.is('android')) { uri_type: this.camera.DestinationType.FILE_URI }
            if (this.platform.is('ios')) { uri_type: this.camera.DestinationType.NATIVE_URI }
                // Testo con un singolo metodo, da provare su iphone se funziona
            //uri_type = this.camera.DestinationType.FILE_URI; // Metodo sicuramente ok per android

            const options: CameraOptions = {
                quality: 100,
                destinationType: uri_type,
                encodingType: this.camera.EncodingType.JPEG,
                mediaType: this.camera.MediaType.PICTURE,
                saveToPhotoAlbum: true,
            }   

This is the code I use to take the actual picture and upload to remote file (as stated, it works fine)

            this.camera.getPicture(options).then( res => {

                // Recuperp il path dal parametro "res"
                let path:string = res.toString();

                // Estraggo il nome e il percorso del file
                var currentName = path.substr(path.lastIndexOf('/') + 1);
                var correctPath = path.substr(0, path.lastIndexOf('/') + 1);

                if ( CONFIG.DEV == 1) { 
                    let datetime = new Date();
                    console.log('[objects-dashboard] @ ' + datetime.toISOString() + ' picture path: ');
                    console.log(path);
                }

                // Leggo il contenuto in un buffer
                //this.file.readAsArrayBuffer(correctPath.toString(), currentName)
                //this.file.readAsDataURL(correctPath.toString(), currentName)
                this.file.readAsArrayBuffer(correctPath.toString(), currentName)
                .then( res => {

                    // E uso il contenuto per creare un blob con il binario del file
                    let blob = new Blob([res], {type: "image/jpeg"});
                    if ( CONFIG.DEV == 1) { 
                        let datetime = new Date();
                        console.log('[objects-dashboard] @ ' + datetime.toISOString() + 'data blob: ');
                        console.log(blob);
                    }
                    // invoco il metodo per caricare il file
                    this.uploadFile(blob, currentName)

                });

According to cordova plugin docs, the option saveToPhotoAlbum should save the picture take automatically into the photo gallery of the phone using the default album for both Android and iOS. But nothing happens.

Don't know if the info can be useful, but this is the list of relevant plugins:

  • cordova-plugin-camera 4.0.3 "Camera"
  • cordova-plugin-device 2.0.2 "Device"
  • cordova-plugin-file 6.0.1 "File"
  • cordova-plugin-file-transfer 1.7.1 "File Transfer"

Thanks in advance for any heads-up


Solution

  • Turns out I had to do a couple of reinstallations of various plugins in order to obtain the desired behaviour.

    I had to:

    • remove and reinstall camera plugin
    • remove and re-add ios platform
    • reinstall the app on the device via xcode (to force permission request again)
    • remove the cordova-photo-library plugin and the cordova-plugin-add-swift-support
    • re-add cordova-plugin-add-swift-support and cordova-photo-library using:
    cordova plugin add https://github.com/nilebma/cordova-plugin-photo-library.git
    

    Using this version of the plugin did the real trick: now I can save photos from the app in a specific album that has the same name of the app, in order to keep the photos separated from the Photo Reel.

    I've created this function

            saveImage(path) {
                this.photolib.requestAuthorization()
                .then ( res => {
                    this.photolib.saveImage(path, 'T-Site')
                    .then( data => {
                        if (CONFIG.DEV == 1) {
                            let datetime = new Date();
                            console.log('[objects-dashboard] @ ' + datetime.toISOString() + 'PhotoLibrary data: ');
                            console.log(data);
                        }   
                    });
                });
            }
    

    in order to do that

    Thanks for the help