我有这个应用程序,我拍照或从库中选择它然后将其移动到文件系统,将图像的imgpath保存到sqlite数据库并显示它,到目前为止一切都很好,现在当我从中删除imgpath sqlite db,我想同时从文件系统中删除它。
$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);
}
);
}
答案 0 :(得分:1)
首先下载插件 - cordova插件添加cordova-plugin-file
然后执行以下代码
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
});
});
});
以下是完整的文档:https://github.com/apache/cordova-plugin-file/#where-to-store-files