缓存图片网址cordova文件和相机

时间:2017-01-23 10:09:28

标签: angularjs cordova ionic-framework

我使用离子angularJS创建了应用程序,设备用于打开相机并使用$ cordova文件捕获图像和设备文件并将其显示在HTML上。 现在,每当我关闭应用程序并再次重新打开时,图像就会消失,我该如何缓存它或将图像URL保存到本地存储(而不是图像本身)。

       $scope.takePhoto = function() {                  
           var options = {                    
               quality: 75,
               destinationType: Camera.DestinationType.FILE_URI,
                sourceType: Camera.PictureSourceType.CAMERA,
               allowEdit: true,
               encodingType: Camera.EncodingType.JPEG,
               correctOrientation: true,
               targetWidth: 100,
               targetHeight: 100,
               popoverOptions: CameraPopoverOptions, // for IOS and IPAD
               saveToPhotoAlbum: false                
           };                      
           $cordovaCamera.getPicture(options).then(function(imageData) {                    
               onImageSuccess(imageData);

               function onImageSuccess(fileURI) {
                   createFileEntry(fileURI);
               }

               function createFileEntry(fileURI) {
                   window.resolveLocalFileSystemURL(fileURI, copyFile, fail);
               }

               //5
               function copyFile(fileEntry) {
                   var name = fileEntry.fullPath.substr(fileEntry.fullPath.lastIndexOf('/') + 1);
                   alert(name);
                   alert(fileEntry.fullPath);
                   var newName = makeid() + name;
                   alert(newName);

                   window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(fileSystem2) {
                           fileEntry.copyTo(
                               fileSystem2,
                               newName,
                               onCopySuccess,
                               fail
                           );
                       },
                       fail);
               }

               //6
               function onCopySuccess(entry) {
                   $scope.$apply(function() {

                       alert(entry.nativeURL);

                       // Display your captured image in the <img> tag**

                       $scope.urlImg = entry.nativeURL;




                   });
               }

               function fail(error) {
                   console.log("fail: " + error.code);
               }

               function makeid() {
                   var text = "";
                   var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

                   for (var i = 0; i < 5; i++) {
                       text += possible.charAt(Math.floor(Math.random() * possible.length));
                   }
                   return text;
               }

           }, function(err) {
               console.log(err);
           });

       }

       $scope.urlForImage = function(imageName) {
           var name = imageName.substr(imageName.lastIndexOf('/') + 1);
           var trueOrigin = cordova.file.dataDirectory + name;
           return trueOrigin;
       }

HTML

             <ion-scroll>
                  <img ng-src="{{urlImg}}" height="200px"/>
              </ion-scroll>

0 个答案:

没有答案