angularjs - Cordova delete files after sqlite delete it -
i have app take picture or select gallery move file-system , save imgpath image sqlite db , display it, works great far , when delete imgpath sqlite db , want delete file-system @ same time .
$scope.deleteimg = function(imgpath) { if (!imgpath) { console.error("no filename specified. file not 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 delete file '" + imgpath.id + "'", json.stringify(err)); }); }, function(err) { console.log("image '" + imgpath.id + "' not exist", json.stringify(err)); }); } $scope.add = function(path) { console.log("i adding db path: " + path); $cordovasqlite.execute(db, "insert imagetable (image) values(?)", [path]).then( function(res) { console.log("i done adding db"); } ); }, function(e) { alert(e); }; $scope.showalldata = function() { console.log("i reading db"); $scope.images = []; $cordovasqlite.execute(db,"select * imagetable order id desc" ).then( function(res) { console.log("i finished reading db"); if (res.rows.length > 0) { (var = 0; < 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] - image name: ' + name); var sql = "select * imagetable 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 1 image returned!!!! ' + name); } else { $scope.delete(res.rows.item(0).id); $scope.deleteimg(imagepath.id); } } else { console.log('[getimgidbyname] - no image found name: ' + name); } }, function(error) { alert("error occured: " + error.message); } ); }
first download plugin - cordova plugin add cordova-plugin-file
and execute following code
var path = cordova.file.applicationdirectory + '/www/res/image'; // absolute path var filename = "image.png"; window.resolvelocalfilesystemurl(path, function(dir) { dir.getfile(filename, {create:false}, function(fileentry) { fileentry.remove(function(){ // file has been removed succesfully },function(error){ // error deleting file },function(){ // file doesn't exist }); }); });
here complete documentation : https://github.com/apache/cordova-plugin-file/#where-to-store-files
Comments
Post a Comment