Show image in gallery

1

I'm developing a mobile app with Apache Cordova , where I download some images, I can download, but the images do not go to the gallery. I have tried to save the images inside the Pictures folder, the images only appear in the File Manager of my Android, when the phone is connected to the computer the images do not appear.

 function download(URL, Folder_Name, File_Name) {
        //step to request a file system 
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);

        function fileSystemSuccess(fileSystem) {
            var download_link = encodeURI(URL);
            var ext = download_link.substring(download_link.lastIndexOf('.') + 1); //Get extension of URL
            var directoryEntry = fileSystem.root; // to get root path of directory
            directoryEntry.getDirectory(Folder_Name, { create: true, exclusive: false }, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard
            var rootdir = fileSystem.root;
            var fp = rootdir.toURL(); // Returns Fulpath of local directory
            fp = fp + "/" + Folder_Name + "/" + File_Name; // fullpath and name of the file which we want to give
            // download function call                
            filetransfer(download_link, fp);
        }

        function onDirectorySuccess(parent) {
            //alert("Sucesso");
        }

        function onDirectoryFail(error) {
            //Error while creating directory
            alert("Unable to create new directory: " + error.code);
        }

        function fileSystemFail(evt) {
            //Unable to access file system
            alert(evt.target.error.code);
        }
    }
        function filetransfer(download_link, fp) {
            var fileTransfer = new FileTransfer();
            // File download function with URL and local path
            fileTransfer.download(download_link, fp,
                                function (entry) {
                                    alert("download complete: " + entry.fullPath);
                                },
                             function (error) {
                                 //Download abort errors or download failed errors
                                 console.log(error);
                                 alert(error.exception);
                                 alert("download error source " + error.source);
                                 //alert("download error target " + error.target);
                                 //alert("upload error code" + error.code);
                             }
                        );
        }
    
asked by anonymous 14.04.2015 / 02:28

1 answer

0

Images were being saved but needed the Media Scanner to index the images in the gallery

Media Scanner:

  

MediaScannerConnection provides a way for pass-through applications   a newly created media file or downloaded to the scanner service   from media. The MediaScanner service will read the file metadata and   add the file to the media content provider.

As I was working with Apache Cordova / PhoneGap it does not provide any native methods for refreshing the images in the native gallery, so I needed to look for some plugins to do this work. The plugins I found were:

cordova-mediascanner-plugin
MediaScannerPlugin

Both have basic documentation, however I used the cordova-mediascanner-plugin .

When implementing this plugin, I only modified my method filetransfer

function filetransfer(download_link, fp) {
        var fileTransfer = new FileTransfer();
        console.log(fp);

        // File download function with URL and local path
        fileTransfer.download(download_link, fp,
                            function (entry) {
                                //alert("download complete: " + entry.fullPath);
                                window.plugins.scanmedia.scanFile(fp, function (msg) {
                                    alert("Success ScanMedia");
                                }, function (err) {
                                    alert("Fail ScanMedia: " + err);
                                })
                            },
                         function (error) {
                             //Download abort errors or download failed errors
                             console.log(error);
                             alert(error.exception);
                             alert("download error source " + error.source);
                             //alert("download error target " + error.target);
                             //alert("upload error code" + error.code);
                      }
                    );

    }
    
16.04.2015 / 14:37