Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
限制上传的图像大小摄像头插件cordova ionic_Cordova_Ionic Framework_Cordova Plugins - Fatal编程技术网

限制上传的图像大小摄像头插件cordova ionic

限制上传的图像大小摄像头插件cordova ionic,cordova,ionic-framework,cordova-plugins,Cordova,Ionic Framework,Cordova Plugins,我在ionic应用程序中使用摄像头插件获取了这张图像。我想限制用户选择的图像大小,比如200kb。我添加了摄像头作为文件插件 我使用了以下代码: /*Function to get image from gallery*/ $scope.getImageFromGallery = function(){ var options = { quality: 100, destinationType: Camera.DestinationType.FILE_UR

我在ionic应用程序中使用摄像头插件获取了这张图像。我想限制用户选择的图像大小,比如200kb。我添加了摄像头作为文件插件

我使用了以下代码:

/*Function to get image from gallery*/
$scope.getImageFromGallery = function(){
    var options = {
        quality: 100,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false,
        targetWidth: 450,
        targetHeight: 450,
        encodingType: Camera.EncodingType.JPEG,
    };

    navigator.camera.getPicture(gallerySuccess, galleryError, options);

    function gallerySuccess(imageURI){
        getSize(imageURI);          
    }


    function getSize(fileUri) {
        window.resolveLocalFileSystemURL(fileUri, function(fileEntry){

            fileEntry.getMetadata(function(metadata){
                console.log("size is "+metadata.size);
            }, resOnError);

            fileEntry.file(function(file) {

                var reader = new FileReader();
                reader.onloadend = function(evt) {

                    var imgData = evt.target.result;
                    var res = imgData.split(",");
                    $rootScope.imagebase64 = res[1];
                    var image = document.getElementById('preview-image1');
                    image.src = evt.target.result;
                    $('#preview-image').css('display','block');
                    $('#preview-image').css('background-image', "url("+res[1]+")").show();
                };

                reader.readAsDataURL(file);
            }, resOnError);
        },
        resOnError);
    }

    function resOnError(error) {
        console.log("error "+JSON.stringify(error));
    }

    function galleryError(error) {
    }
}
但在这里,当我选择2.5MB的图像时,它显示的是178908大小 当我选择8.9MB大小的图像时,它显示的是88412大小


据我所知,文件大小是以字节为单位的,但我得到的值不正确。

试试这个解决方案这是150kb的,你只需将它改为200kb即可

  window.resolveLocalFileSystemURI(newPath, function (fileEntry) {
                        fileEntry.file(function (fileObj) {
                            if (fileObj.size <= 153600) {
                                var reader = new FileReader();
                                reader.onload = function () {
                                    var dataURL = reader.result;
                                    //your stuff.....
                                };
                                reader.readAsDataURL(fileObj);
                            }
                            else {
                                //  alert("Please upload image less then 150KB");
                                alertPopup = $ionicPopup.alert({
                                    title: 'Upload image failed!',
                                    template: 'Please upload image less then 150KB!'
                                });
                            }

                            console.log("Size = " + fileObj.size);
                        });
                    });
window.resolveLocalFileSystemURI(新路径,函数(fileEntry){
fileEntry.file(函数(fileObj){
如果(fileObj.size