Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova/PhoneGap Photo File Size

Tags:

cordova

All,

I'm trying to get the file size (i.e. not the dimensions, but the actual file size in disk) of a photo using PhoneGap/Cordova 2. So far, the only way I can figure it out is through base64 conversion, then basic arithmetic on the bytes of the b64 string. However, I was wondering if there is a more elegant way to get file size. TIA.

clarification: it must work on iOS 5 and Android 2.3.

like image 583
rdodev Avatar asked Aug 16 '12 18:08

rdodev


1 Answers

Yeah, if you have the URL to the image from the Camera.getPicture command you pass it to the window.resolveLocalFileSystemURL command who's success callback is called with a FileEntry object. Then you call the "file" method of FileEntry which calls the success callback with a File object then you can look at the size property of the File object.

Something like....

function takePhoto() {
    navigator.camera.getPicture(gotPhoto, onError, { quality: 50,
        destinationType: navigator.camera.DestinationType.FILE_URI,
        sourceType: navigator.camera.PictureSourceType.CAMERA });
}

function gotPhoto(imageUri) {
    window.resolveLocalFileSystemURI(imageUri, function(fileEntry) {
        fileEntry.file(function(fileObj) {
            console.log("Size = " + fileObj.size);
        });
    });
}

that should work but I just wrote the code off the top of my head.

like image 132
Simon MacDonald Avatar answered Oct 19 '22 21:10

Simon MacDonald