Cordova delete files after sqlite delete it

2019-09-14 07:23发布

问题:

I have this app where I take picture or select it from gallery then move it into file-system , save the imgpath for the image into sqlite db and display it, everything works great so far , now when I delete the imgpath from the sqlite db , I want to delete it from file-system at the same time .

$scope.deleteImg = function(imgPath) {


    if (!imgPath) {
        console.error("No fileName specified. File could not be deleted.");
        return false;
    } else {


        $cordovaFile.checkFile(cordova.file.externalRootDirectory, imgPath.id).then(function() {
            $cordovaFile.removeFile(cordova.file.externalRootDirectory, imgPath.id).then(function(result) {
                console.log("image '" + imgPath + "' deleted", JSON.stringify(result));
            }, function(err) {
                console.error("Failed to delete file '" + imgPath.id + "'", JSON.stringify(err));

            });
        }, function(err) {
            console.log("image '" + imgPath.id + "' does not exist", JSON.stringify(err));
        });

    }

    $scope.add = function(path) { 
             
            console.log("I AM ADDING TO DB WITH PATH: " + path);   

              
            $cordovaSQLite.execute(db, "INSERT INTO imageTable (image) VALUES(?)", [path]).then(
                function(res) {
                    console.log("I AM DONE ADDING TO DB");   
                }
            );                


                  
        },
        function(e) {        
            alert(e);      
        };



        

      
    $scope.ShowAllData = function() {
        console.log("I AM READING FROM DB");   
        $scope.images = [];             
        $cordovaSQLite.execute(db,"SELECT * FROM imageTable ORDER BY id DESC"  ).then(        function(res) {

            console.log("I FINISHED READING  FROM DB");   

                      
            if (res.rows.length > 0) {            
                for (var i = 0; i < res.rows.length; i++) {              
                    $scope.images.push({
                        id: res.rows.item(i).id,
                        image: res.rows.item(i).image

                                      
                    });

                }          
            } 
            return $scope.images;

                    
        },         function(error) {          
            alert(error);        
        }      );

            
    }  

    $scope.getImgIDbyName = function(name) {

        console.log('[getImgIDbyName] - get image with name:  ' + name);
        var sql = "SELECT * FROM imageTable WHERE image = '" + name + "';";
        console.log(sql);
        $cordovaSQLite.execute(db,
            sql
        ).then(
            function(res) {
                if (res.rows.length > 0) {
                    if (res.rows.length > 1) {
                        console.log('[getImgIDbyName] - OOPS more than 1 image returned!!!!  ' + name);
                    } else {
                        $scope.delete(res.rows.item(0).id);
                        $scope.deleteImg(imagePath.id);
                    }
                } else {
                    console.log('[getImgIDbyName] - no image found with name:  ' + name);
                }
            },
            function(error) {
                alert("error occured: " + error.message);
            }
        );



    }

回答1:

First download the plugin - cordova plugin add cordova-plugin-file

and then execute the following code

var path = cordova.file.applicationDirectory + '/www/res/image'; // get the absolute path
var filename = "image.png";

window.resolveLocalFileSystemURL(path, function(dir) {
    dir.getFile(filename, {create:false}, function(fileEntry) {
              fileEntry.remove(function(){
                  // The file has been removed succesfully
              },function(error){
                  // Error deleting the file
              },function(){
                 // The file doesn't exist
              });
    });
});

Here is the complete documentation : https://github.com/apache/cordova-plugin-file/#where-to-store-files