android - 从 cordova 中的 MediaType.ALLMEDIA 获取 "file:///storage"路径

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

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

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

mediaType: Camera.MediaType.ALLMEDIA

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

最佳答案

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

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

关于android - 从 cordova 中的 MediaType.ALLMEDIA 获取 "file:///storage"路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43719158/

相关文章:

javascript - 用它来定义 Angular 函数

ios - Wizcorp/phonegap-facebook-插件 : building ios failed

javascript - 在断开连接的状态下运行 jquery 移动网络应用程序?

php - 我无法使用 AJAX (XMLHttpRequest) 获取 responseXML

java - 即使用户切换应用程序,CountDownTimer 也会运行

cordova - 将插件添加到 Phonegap 时出错

ajax - 使用自签名证书对 HTTPS 服务器的 PhoneGap Ajax 调用失败

javascript - ionic v1 类型 ="date"

java - 在 Android 中搜索从 XML 解析的数组

Android 5.0.2 及以上版本不允许通过蓝牙 LE 访问 HID