Restrict uploaded image size Camera plugin cordova ionic

谁都会走 提交于 2019-12-09 12:53:56

问题


I have fetched the image using Camera plugin in my ionic app. I want to restrict the user on the size of the image which user is choosing, lets say 200kb. I have added Camera as File plugin.

I have used below code:

/*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) {
    }
}

But here, when I'm choosing image of 2.5MB, it is showing size 178908 and when I'm choosing image of size 8.9MB, it is showing size 88412.

As per my knowledge, the file size is in bytes, but the values which I'm getting are not correct.


回答1:


try this solution this is for 150kb for yours just change it to 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);
                        });
                    });


来源:https://stackoverflow.com/questions/46075271/restrict-uploaded-image-size-camera-plugin-cordova-ionic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!