Search code examples
androidcordovacordova-plugins

Cordova Plugins Photo Library Plugin: Get downloaded picture file_uri path


I am using Cordova Plugins Photo Library Plugin to download picture from server or internet. I successfully downloaded picture from internet and server. However, I fail to get the path of picture. I need the file_uri path so that I can display the picture by showing the file_uri path.

The following is my code.

 var url = 'https://images-cdn.9gag.com/photo/am9znpv_700b.jpg'; 
    var album = 'DRDMChat';
    cordova.plugins.photoLibrary.saveImage(url, album, 
        function (libraryItem) {
            var ImagePath = libraryItem.cdvphotolibrary
            alert(libraryItem.cdvphotolibrary);
        }, function (err) {
            alert(err);
        });

Solution

  • You can get the path to the photo using the photoURL attribute of libraryItem.

    Here is the code -

    var url = 'https://images-cdn.9gag.com/photo/am9znpv_700b.jpg'; 
    var album = 'DRDMChat';
    cordova.plugins.photoLibrary.saveImage(url, album, 
        function (libraryItem) {
    
            //log or alert the below attributes
            console.log(libraryItem.id);          // ID of the photo 
            console.log(libraryItem.photoURL);    // Cross-platform access to photo 
            console.log(libraryItem.thumbnailURL);// Cross-platform access to thumbnail 
            console.log(libraryItem.fileName);
    
            var ImagePath = libraryItem.photoURL //Photo URL Access
            alert(libraryItem.photoURL);
        }, function (err) {
            alert(err);
        });
    

    Refer Displaying photos Section of cordova-plugin-photo-library for other attributes of libraryItem.