Cordova图像文件URL保存到SQL

Cordova图像文件URL保存到SQL,sql,angularjs,cordova,ionic-framework,Sql,Angularjs,Cordova,Ionic Framework,我有一个应用程序,在这个应用程序中,我拍摄图像,将其保存到本地文件系统,然后“临时”显示,我尝试将图像的文件url保存在SQL(sqlcipher)上,然后为每个图像分配id并“永久”显示,最后将它们上载到服务器。到目前为止,我可以捕获图像并将其保存在应用程序的文件系统(缓存文件夹)中。我尝试了几种方法来实现sql部分,但仍然不知道如何将其连接到dom(html)并使其工作 任何帮助都将不胜感激 控制器 $cordovaCamera.getPicture(options).then(funct

我有一个应用程序,在这个应用程序中,我拍摄图像,将其保存到本地文件系统,然后“临时”显示,我尝试将图像的文件url保存在SQL(sqlcipher)上,然后为每个图像分配id并“永久”显示,最后将它们上载到服务器。到目前为止,我可以捕获图像并将其保存在应用程序的文件系统(缓存文件夹)中。我尝试了几种方法来实现sql部分,但仍然不知道如何将其连接到dom(html)并使其工作

任何帮助都将不胜感激

控制器

 $cordovaCamera.getPicture(options).then(function(imagePath) {
    // Grab the file name of the photo in the temporary directory

    var currentName = imagePath.replace(/^.*[\\\/]/, '');
    alert(currentName);

    //Create a new name for the photo to avoid duplication

    var d = new Date(),
        n = d.getTime(),
        newFileName = n + ".jpg";

    // If you are trying to load image from the gallery on Android we need special treatment!

    if ($cordovaDevice.getPlatform() == 'Android' && sourceType === Camera.PictureSourceType.PHOTOLIBRARY) {
        window.FilePath.resolveNativePath(imagePath, function(entry) {
            window.resolveLocalFileSystemURL(entry, success, fail);

            function fail(e) {
                console.error('Error: ', e);
            }

            function success(fileEntry) {
                var namePath = fileEntry.nativeURL.substr(0, fileEntry.nativeURL.lastIndexOf('/') + 1);
                // Only copy because of access rights
                $cordovaFile.copyFile(namePath, fileEntry.name, cordova.file.dataDirectory, newFileName).then(function(success) {
                    $scope.image = newFileName;
                }, function(error) {
                    $scope.showAlert('Error', error.exception);
                });
                alert(fileEntry.nativeURL);

            };
        });
    } else {
        var namePath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
        // Move the file to permanent storage
        $cordovaFile.moveFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(function(success) {
            $scope.image = newFileName;
        }, function(error) {
            $scope.showAlert('Error', error.exception);
        });
        alert(namePath);

    }
},
function(err) {
    // Not always an error, maybe cancel was pressed...
})
};

 // Returns the local path inside the app for an image
  $scope.pathForImage = function(image) {
  if (image === null) {
    return '';
   } else {
    return cordova.file.dataDirectory + image;

}
  };
SQL

$cordovaSQLite.execute(_db,'CREATE TABLE IF NOT EXISTS Image'+
'(' +
'g_id整数主键,'+
'图像URL文本,'+
“时间戳文本”+
')');
函数SaveImgInDB(jsonImgObj){
var imgObj={};
imgObj.g_id=数据检查(jsonImgObj.g_id);
imgObj.imageUrl=DataCheck(jsonImgObj.imageUrl);
imgObj.timeStamp=DataCheck(jsonImgObj.timeStamp);
InsertImgInDB(imgObj);
}
函数InsertImgInDB(imgObj){
变量查询='插入图像'+
'(' +
'g_id整数主键,'+
'图像URL文本,'+
“时间戳文本”+
')';
$cordovaSQLite.execute(_db,query[
imgObj.g_id,
imgObj.imageUrl,
imgObj.时间戳
]).然后(功能(res){
Logger.log(“Img Insert/ID->”+res.insertId);
},
功能(err){
Logger.log('img Insert'+错误消息);
});
}
.factory('Images',函数($cordovaSQLite){
var图像=[];
返回{
全部:函数(){
$cordovaSQLite.execute(_db,“从图像中选择*)
.然后(功能(res){
对于(var i=0;i
HTML


要将图像数据获取到DOM,请在控制器中调用image.all()方法获取图像。因为这是一个SQL调用,所以必须将其包装在如下承诺中:

all: function() {
    return $q(function(resolve, reject){
        $cordovaSQLite.execute(_db, "SELECT * FROM Image")
                  .then(function(res) {
                          for (var i = 0; i < res.rows.length; i++) {
                              images.push(res.rows.item(i));
                          }
                          return resolve(images);//sending back images once the sql loads the dataset
                      },
                      function(err) {
                          console.log("Error");
                          return reject(err);//throwing error when there is a db error
                      })

    })

}
在html中: 编辑:


//对所有图像重复img标记,并将其URL绑定到img标记

您好,谢谢您的回答,但我正在使用文件url通过pathForImage函数获取图像。因此,我应该在控制器的哪个部分绑定承诺?您可以在控制器本身的开头调用承诺。抱歉,因此请在编辑中发布。。基本上,将从ngrepeat获得的图像传递到pathForImage函数。。另外,如果你觉得我的答案有帮助,请将其标记为正确:-)好的,我会的,最后一件事,我应该在控制器的哪一部分约束承诺
<ion-scroll>
    <img ng-src="{{pathForImage(image)}}" style="width: 100%; height: 100%;" />
</ion-scroll>
all: function() {
    return $q(function(resolve, reject){
        $cordovaSQLite.execute(_db, "SELECT * FROM Image")
                  .then(function(res) {
                          for (var i = 0; i < res.rows.length; i++) {
                              images.push(res.rows.item(i));
                          }
                          return resolve(images);//sending back images once the sql loads the dataset
                      },
                      function(err) {
                          console.log("Error");
                          return reject(err);//throwing error when there is a db error
                      })

    })

}
Image.all()//Image.all will return promise since we have impleented using $q service
    .then(function(images){
         $scope.images = images;//attach the image data to scope variable which can be bound to the DOM.
    })
    .catch(function(error){
         alert("couldnt load images");
    })
<ion-scroll>
    <img ng-repeat="image in images" ng-src="pathForImage(image)" style="width: 100%; height: 100%;" />//repeat img tag for all images and bind their urls to the img tag
</ion-scroll>