【问题标题】:get "file:///storage" path from MediaType.ALLMEDIA in cordova从 Cordova 中的 MediaType.ALLMEDIA 获取“file:///storage”路径
【发布时间】:2017-05-01 20:52:10
【问题描述】:

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

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

当我使用 媒体类型:Camera.MediaType.PICTURE,

在上面的代码中,它将文件路径返回为“file:///storage/emulated/0/....”并且工作正常。 但由于我需要上传所有类型的文件,所以我将上面的行替换为

mediaType: Camera.MediaType.ALLMEDIA

并且这个文件路径变为“/storage/emulated/0/...”,然后它不进入“window.resolveLocalFileSystemURL”函数。 那么有没有办法将后面的路径转换为上面提到的类似路径?

【问题讨论】:

    标签: android cordova ionic-framework


    【解决方案1】:

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

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

    【讨论】:

    • 这是我使用“mediaType:Camera.MediaType.ALLMEDIA”获得的路径 - /storage/emulated/0/download/unnamed.png。并且使用“mediaType:Camera.MediaType.PICTURE”路径是 - 文件 ///storage/emulated/0/android/data/com.myapp/cache/unnamed.png
    • 因此,当您使用 Camera.MediaType.ALLMEDIA 时,只需使用我的答案将 file:// 附加到路径。
    • 但是两者的路径不同,附加file://无效,请注意文件路径。
    • 它起作用的原因是因为它是同一个文件,但是当您使用 PICTURE 时,相机插件会在您的应用程序缓存中为您提供文件,然后由您来保存文件是否编辑它。使用 ALLMEDIA 时,插件实际上会返回直接文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多