2016-03-14 80 views
0

我試圖從使用$cordovaCamera插件或從圖庫中拍攝的照片中讀取Image Data Url Content,但我無法使其工作。它總是以Error Code 5: ENCODING_ERR返回。我也找不到它的意思。Ionic讀取圖像數據網址

我當前的代碼如下

var options = { 
    quality: 50, 
    destinationType: Camera.DestinationType.FILE_URI, 
    sourceType: Camera.PictureSourceType.CAMERA, 
    allowEdit: false, 
    correctOrientation: true, 
    encodingType: Camera.EncodingType.JPEG, 
    saveToPhotoAlbum: false 
}; 

$cordovaCamera.getPicture(options) 
    .then(function (imageURI) { 
    var indexOfLash = imageURI.lastIndexOf('/') + 1; 
    var name = imageURI.substr(indexOfLash); 
    var namePath = imageURI.substr(0, indexOfLash); 

    return $cordovaFile.copyFile(namePath, name, cordova.file.dataDirectory, name); 
    }) 
    .then(function (info) { 
    console.log('copied', info); 
    return $cordovaFile.readAsDataURL(cordova.file.dataDirectory, info.nativeURL); 
    }) 
    .then(function (success) { 
    // success 
    console.log("image data", success); 
    }) 
    .then(function() { 
    $scope.profile = user.getProfile(); 
    }) 
    .catch(function (error) { 
    console.log('Failed because', error); 
    }); 

回答

0

安裝這個插件 科爾多瓦插件添加org.apache.cordova.camera

// EDIT PROFILE IMG CHANGE 
    $scope.editProfileImgGallary = function() { 
    navigator.camera.getPicture(uploadEditProfilePhotosImage, onFailEditProfilePhoto, { 
     targetWidth: 512, 
     targetHeight: 512, 
     quality: 80, 
     correctOrientation: true, 
     allowEdit: true, 
     destinationType: navigator.camera.DestinationType.FILE_URI, 
     sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY 
    }); 
    } 

    function onFailEditProfilePhoto(message) { 
    // alert('Failed because: ' + message); 
    } 

    function uploadEditProfilePhotosImage(imageURI) { 
    $ionicLoading.show({ 
     template: '<p>Loading...</p><ion-spinner icon="bubbles"></ion-spinner>' 
    }); 
    console.log(imageURI); 
    var img = document.getElementById('userEditProfileImg'); 
    img.src = imageURI; 
    var options = new FileUploadOptions(); 
    options.fileKey = "file"; 
    options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1); 
    options.mimeType = "image/jpeg"; 
    var ft = new FileTransfer(); 
    ft.upload(imageURI, encodeURI("http://XYZ/Xyz.php?uid=" + userId + ""), winEditProfilePhotos, failEditProfilePhotos, options); 
    } 

    function winEditProfilePhotos(r) { 
    console.log("Code = " + r.responseCode); 
    console.log("Response = " + r.response); 
    console.log("Sent = " + r.bytesSent); 
    $ionicLoading.hide(); 
    } 

    function failEditProfilePhotos(error) { 
    console.log("upload error source " + error.source); 
    console.log("upload error target " + error.target); 
    $ionicLoading.hide(); 
    var alertPopup = $ionicPopup.alert({ 
     title: 'Uh Oh!', 
     template: 'You Get Some Error Please Try Again..' 
    }); 
    } 

    // END = EDIT PROFILE IMG CHANGE 
+0

最後,您正在使用FileTransfer上傳圖像,但我想打開它並以base64格式讀取其內容,相機插件是不是問題,是如何讀取圖像 – bitgandtter

0

我能夠讀取使用FULLPATH不是文件nativeURL,這裏是代碼片段

$cordovaFile.readAsDataURL(cordova.file.dataDirectory, info.fullPath.substring(1)); 

注意前導「/」的減法,因爲它會引起錯誤