0
votes

I am building an Ionic app with camera plugin. The problem that I need to solve is following as below:

  1. Execute Ionic App
  2. Open the camera via cordova-plugin-camera
  3. Take a photo and retrieve data with FILE_URI, but in step 3, when I receive the data with URI, the file path is like this:

    file:///sdcard/emulated/0/..../142312321.jpg
    
  4. So I save this uri to $scope.photo_uri and set the <img/> tag to

    <img src="{{photo_uri}} />
    

But no photo displayed. How can I solve this?

The codes are following as below:

$scope.onTakePhoto = function() {

    if (typeof navigator.camera == 'undefined'){
        alert("We'r sorry! Please try again on your Phone.^_^");
        return;
    }

    var options = {
        quality : 75,
        destinationType : Camera.DestinationType.NATIVE_URI,
        sourceType : Camera.PictureSourceType.CAMERA,
        allowEdit : true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 300,
        targetHeight: 300,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false
    };

    $cordovaCamera.getPicture(options).then(function (imageUri) {
        //try{
        var id = 'P_' + (new Date()).Format('MM_dd') + '_' + ($scope.photoList.length + 1);
        //var data = "data:image/jpeg;base64," + imageData;
        var date = (new Date()).Format('yyyy-MM-dd hh:mm P');
        var name = 'PM_IMG_' + (new Date()).Format('yyyyMMddhhmmss') + '.jpg';
        var location = gMyLocation.getData();
        var uri = $scope.folder.uri;

        window.resolveLocalFileSystemURL(imageUri, function(fileEntry) {

            //If this doesn't work
            $scope.camera_image_data.image = fileEntry.nativeURL;

            console.log($scope.camera_image_data.image);
            //Try this
            //var image = document.getElementById('myImage');
            //image.src = fileEntry.nativeURL;
        });

        //$scope.camera_image_data.image = 'cdv' + imageUri;
        //alert('DataURI : '+ imageUri);

        //createPhotoFile($scope.folder.uri, name, data, true);
        //movePhotoFile(imageUri, uri, name);

        var sPhoto = new PhotoObj(id,name,date,location,uri);
        $scope.photoList.push(sPhoto);
        //alert("PATH : " + $scope.photoList[0].uri +'\n'+
        //'NAME : ' + $scope.photoList[0].name);

    }, function (err) {
        // error
        console.log(err);
    });
};
1

1 Answers

0
votes

Move the file(captured from camera) to cordova.file.externalRootDirectory and get and save the file(moved) path. Here is the movePhotoFile() function.

function movePhotoFile(oriPathStr, distPath, name) {
        var oriName = oriPathStr.substr(oriPathStr.lastIndexOf('/') + 1);
        var oriPath = oriPathStr.substr(0, oriPathStr.lastIndexOf('/') + 1);

        alert(oriPath);

        $cordovaFile.moveFile(oriPath, oriName, distPath, name)
            .then(function (success) {
                // success
                console.log("Correctly Moved your File!!!");
                // here is your code 

            }, function (error) {
                // error
                console.log("Failed Move Image!! 0.0")
            });
    }

And please ionic run android or build your app and test with the APK.