get" file:/// storage"来自cordova中的MediaType.ALLMEDIA的路径

时间:2017-05-01 12:32:43

标签: android cordova ionic-framework

我需要在我的应用程序中上传所有类型的文件,并且我需要获取图像高度和宽度以及我正在使用的图像:

$scope.uploadFile = function(){
        $scope.imageUploading = true;
         var options = {
            quality: 70,
            //~ targetWidth: 1005,
            //~ targetHeight: 693,
            destinationType: Camera.DestinationType.FILE_URI,
            sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
            mediaType: Camera.MediaType.PICTURE,
            correctOrientation: true
        };
      $cordovaCamera.getPicture(options).then(function(imageData) {
         imageData = imageData.split('?');
         var imageURI = imageData[0];
         // This function is called once an imageURI is rerturned from PhoneGap's camera or gallery function
        window.resolveLocalFileSystemURL(imageURI, function(fileEntry) {
            fileEntry.file(function(fileObject){
                // Create a reader to read the file
                var reader = new FileReader();
                // Create a function to process the file once it's read
                reader.onloadend = function(evt) {
                    // Create an image element that we will load the data into
                    var image = new Image()
                    image.onload = function(evt) {
                        // The image has been loaded and the data is ready
                        var image_width = this.width
                        var image_height = this.height
                        if(parseInt(image_width) < confArr.image_sizes.portfolio.large.w || parseInt(image_height) < confArr.image_sizes.portfolio.large.h){
                            Auth.toastMessage($rootScope.appMainLang.formvalidation.upload_resolution_limit.replace('%s',parseInt(confArr.image_sizes.portfolio.large.w)),'long','center');
                            $scope.imageUploading = false;
                            $ionicLoading.hide();
                        }else{
                            $scope.imageUploading = true;
                            $scope.jrCrop(imageURI);
                        }
                        image = null
                    }
                    // Load the read data into the image source. It's base64 data
                    image.src = evt.target.result
                }
                // Read from disk the data as base64
                reader.readAsDataURL(fileObject)
            }, function(){
                Auth.toastMessage("There was an error reading or processing this file.","long", "center");
            })
        })


       }, function(err) {
           $scope.imageUploading = false;
           $ionicLoading.hide();
        // Auth.toastMessage(Auth.getlocal("timeoutText","string"),"long", "center");
      });
    }

我用的时候     mediaType:Camera.MediaType.PICTURE,

在上面的代码中,它返回文件的路径为&#34; file:///storage/emulated/0/...."并且正常工作。 但是因为我需要上传所有类型的文件所以我用

替换了上面的行
mediaType: Camera.MediaType.ALLMEDIA

并且使用此文件路径变为&#34; / storage / emulated / 0 /..."然后它不会进入&#34; window.resolveLocalFileSystemURL&#34;功能。 那么有没有办法将这个后面的路径转换为上面提到的路径?

1 个答案:

答案 0 :(得分:2)

只需将file://添加到字符串中即可。像这样:

var imageURI = 'file://' + imageData[0];