title: Ionic Saving Image From Camera With Some Information And Get From Gallery
body: <p>I am working on Ionic mobile application my task is if a person clicks an image from camera the picture get saved with his latitude and longitude and he can choose the image and upload it so the saved picture will have its coordinates with it.</p>
  
  <p>The issue I am facing is when I take picture from camera it saves with an auto generated name. I am able to save latitude and longitude in local storage against that name but when I get that image from gallery it comes up with a different name and path so I am unable to get the information against that image. </p>
  
  <p><strong>My Code of Capturing Image:</strong>  </p>
  
  <pre><code> $scope.takePicture = function(){
          //alert("Hello World");
          var options={
              quality : 75,
              destinationType : Camera.DestinationType.FILE_URI,
              sourceType : Camera.PictureSourceType.CAMERA,
              allowEdit : false,
              encodingType: Camera.EncodingType.JPEG,
              targetWidth: 300,
              targetHeight: 300,
              popoverOptions: CameraPopoverOptions,
              saveToPhotoAlbum: true,
              correctOrientation: true
          };
  
          $cordovaCamera.getPicture(options).then(function (imageData) {
              $scope.showImage = true;
              $scope.showFormPickup = false;
              $scope.showSeatAvaiable = false;
              $scope.showRequestWating = false;
              $rootScope.aloTaxiHeaderBarClass = 'overlay';
  
              //Store File URL in variable
              $scope.fileUrL = imageData;
            $scope.imgURI = imageData;
            //Store File URL in variable
            //alert($scope.imgURI);
  
            var filename= $scope.fileUrL.split("/").pop();
            //alert(filename);
            filename = filename.substr(0,filename.lastIndexOf('.'));
            //alert(filename);
            $scope.fileNameGalery = filename;
            //alert($scope.fileNameGalery);
  
            //alert('File Name:' + filename);
  
            $scope.getLocation();
  
            //alert('Latitude:' + $scope.latitude + 'Longitude:' + $scope.longitude);
  
          },function(err){
              console.log('Error While Taking Pictures:' + err);
          });
      }
    $scope.getLocation=function () {
      //alert("in geo location")
      try {
        var platform = $cordovaDevice.getManufacturer();
        //alert('Device:' + device.isVirtual + 'Platform:' + platform);
        //alert('Platform:' + platform);
        if (platform === 'Genymotion' || platform === 'unknown' || platform == "undefined") {
          //alert('Platform is GennyMotion');
          $scope.latitude = '24.830814';
          $scope.longitude = '67.072883';
        }
        else {
          $scope.getLocationFromGPS();
        }
      }
      catch(err){
        //alert(err);
      }
    }
    $scope.getLocationFromGPS=function () {
      //alert("getLocationFromGPS");
      var posOptions = {
        enableHighAccuracy: true,
        timeout: 20000,
        maximumAge: 0
      };
      $cordovaGeolocation.getCurrentPosition(posOptions).then(function (position) {
        //var lat=position.coords.latitude;
        //var long=position.coords.longitude;
  
        $scope.latitude=position.coords.latitude;
        $scope.longitude=position.coords.longitude;
        if($scope.fileNameGalery != "undefined") {
          if (typeof(Storage) != "undefined") {
            //alert("local Storage");
            var myObj = {name: $scope.fileNameGalery, latitude: $scope.latitude, longitude: $scope.longitude};
            //alert(angular.toJson(myObj));
            try {
              localStorage.setItem($scope.fileNameGalery, JSON.stringify(myObj));
            }
            catch(err){
              //alert(err);
            }
          } else {
            //alert("Sorry Browser does not support Local Storage Feature");
          }
        }
        //alert('Latitude:' + $scope.latitude + 'Longitude:' + $scope.longitude);
  
      },function (err) {
        console.log('Error:' + err);
      });
    }
  </code></pre>
  
  <p>for getting the image from gallery code is :</p>
  
  <pre><code>   $scope.choosePhoto = function () {
      var options = {
        quality: 75,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
        allowEdit: false,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 300,
        targetHeight: 300,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false
      };
  
      $cordovaCamera.getPicture(options).then(function (imageData) {
        $scope.showImage = true;
        $scope.showFormPickup = false;
        $scope.showSeatAvaiable = false;
        $scope.showRequestWating = false;
        $rootScope.aloTaxiHeaderBarClass = 'overlay';
        $scope.imgURI = imageData;
        //Store File URL in variable
        $scope.fileUrL = imageData;
  
        //alert("Image URL:" + $scope.imgURI);
  
        var filename= $scope.fileUrL.split("?").pop();
        //alert(filename);
        filename = filename.substr(0,filename.length);
        //alert(filename);
        try {
          var item = angular.fromJson(localStorage.getItem(filename));
          //alert(item);
          $scope.latitude = item["latitude"];
          $scope.longitude = item["longitude"];
  
          //alert($scope.latitude);
          //alert($scope.longitude);
        }
        catch (err)
        {
          //alert(err);
        }
        //alert('File Name:' + filename);
  
       // $scope.getLocation();
  
        //alert('Latitude:' + $scope.latitude + 'Longitude:' + $scope.longitude);
  
        //if(typeof(Storage) != "undefined") {
  
          //var myObj={name:filename,latitude:$scope.latitude,longitude:$scope.longitude};
          //localStorage.setItem(filename,JSON.stringify(myObj));
        //} else {
         // alert("Sorry Browser does not support Local Storage Feature");
       // }
      }, function (err) {
        console.log('Error While getting pictures from gallery' + err);
      });
    }
  </code></pre>