离子相机问题

时间:2016-04-02 08:45:07

标签: cordova camera ionic-framework cordova-plugins

我正在使用相机插件构建一个Ionic应用程序。 我需要解决的问题如下:

  1. 执行Ionic App
  2. 通过cordova-plugin-camera
  3. 打开相机
  4. 拍摄照片并使用FILE_URI检索数据,但在第3步中,当我收到带URI的数据时,文件路径如下:

    file:///sdcard/emulated/0/..../142312321.jpg
    
  5. 所以我将此uri保存到$scope.photo_uri并将<img/>标记设置为

    <img src="{{photo_uri}} />
    
  6. 但没有显示照片。我该如何解决这个问题?

    代码如下:

    $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 个答案:

答案 0 :(得分:0)

将文件(从相机中捕获)移动到cordova.file.externalRootDirectory 并获取并保存文件(移动)路径。 这是movePhotoFile()函数。

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")
            });
    }

离子运行安卓或构建您的应用并使用APK进行测试。