3

I want when pick photo from device if size of photo > 10 MB or video >100 MB it will not pick. So I use RNFetchBlobStat to get size of photo and video. When I get detail info of photo from device it show size = 1.42MB but when RNFetchBlobStat return size is 3466195 B. (ImagePicker fileSize have same result) Here is my code

const MAX_SIZE_PHOTO = 10485760 //10*1024*1024
const MAX_SIZE_VIDEO = 104857600//100*1024*1024
getSizeImg = (type, source) => {
    RNFetchBlob.fs.stat(source.uri.replace('file:///', '').replace('file://', '').replace('file:/', ''))
        .then((stats) => {
            console.log("file size", stats.size)
            if (type === 'video') {
                if (stats.size > MAX_SIZE_VIDEO) {
                    Alert.alert(null, "This video is too large to be uploaded")
                    error()
                    return
                }
                uploadVideo(source, completeHandler, progressHandler, error, diary_id, token)
            } else {
                if (stats.size > MAX_SIZE_PHOTO) {
                    Alert.alert(null, "This image is too large to be uploaded")
                    error()
                    return
                }
                uploadImage(source)
            }
        })
        .catch((err) => {
            console.log("getSize", err)
            error(err)
        })
}

export let choiceImage = (openLibrary, completeHandler, progressHandler, error, closeLibrary, diary_id, token) => {
    console.log("choiceImage", diary_id, token)
    ImagePicker.showImagePicker(optionsImage, (response) => {
        if (response.didCancel) {
            closeLibrary();
        }
        else if (response.error) {
            error()
        }
        else if (response.customButton) {
            console.log('User tapped custom button: ', response.customButton);
        }
        else {
            // openLibrary(response.uri, 'image');
            let source = {
                uri: response.uri,
                name: response.fileName ? response.fileName : 'image.png',
                type: 'image/*',
            };
            getSizeImg('image', source);
        }
    });
}

const isIOS = Platform.OS === 'ios';

/**
 * FUNCTION CHOICE VIDEO FROM LIBRARY
 * @param func
 */
export let choiceVideo = (openLibrary, completeHandler, progressHandler, error, closeLibrary, diary_id, token) => {
    ImagePicker.showImagePicker(optionsVideo, (response) => {
        console.log('Response = ', response);
        if (response.didCancel) {
            closeLibrary();
        }
        else if (response.error) {
            error();
        }
        else if (response.customButton) {
            console.log('User tapped custom button: ', response.customButton);
        }
        else {
            // openLibrary(isIOS ? response.uri : 'file://' + response.path, 'video');
            let source = {
                uri: response.uri,
                name: response.fileName ? response.fileName : "video.mp4",
                type: 'video/*',
            };
            getSizeImg('video', source);
        }
    });
}
Why RNFetchBlobStat return size photo unlike size when get detail info photo from device? How I can fix it?
Trần Hòa
  • 235
  • 4
  • 15
  • maybe you're in wrong directory.. What is the output of stats ? – digit May 11 '18 at 04:31
  • I remove all and hole a photo in my device -> directory not wrong – Trần Hòa May 11 '18 at 07:03
  • @digit sorry but you're right. When ImagePicker return uri and path it isn't exactly. I don't know why but if I have photo in path "sdcard/DCIM/Camera/20180102_175726.jpg" it will return path "file:///storage/emulated/0/Android/data/com.clisk.rouly/files/Pictures/image-77c547d9-9b62-4e4d-875f-97666dc4644a.jpg", error in here – Trần Hòa May 14 '18 at 07:16
  • If you are using `react-native-image-picker` it will return `origURL` you have to use it instead of `uri` this will fix your problem no need to `replace` anything – Liam May 28 '19 at 00:47

1 Answers1

0

You're right. tat, it seems it is not meant to return the size of file recursively. fs.stat does not return correct images size. The issue still open under react-native-fetch-blob lib.

Basically, you can do this by checking the files' size recursively but if we're going to do this in native it might take some time. Here is some code line may help you :

Pranavan SP
  • 1,785
  • 1
  • 17
  • 33
  • When ImagePicker return uri and path it isn't exactly. I don't know why but if I have photo in path "sdcard/DCIM/Camera/20180102_175726.jpg" it will return path "file:///storage/emulated/0/Android/data/com.clisk.rouly/files/Pictures/image-77c547d9-9b62-4e4d-875f-97666dc4644a.jpg", error in here . If I get size from current path it return exactly file size – Trần Hòa May 18 '18 at 02:44
  • @TrầnHòa I truncated the path to `"storage/emulated/0/Android/data/com.clisk.rouly/files/Pictures/image-77c547d9-9b62-4e4d-875f-97666dc4644a.jpg"` and try error will go. – Pranavan SP May 18 '18 at 04:57
  • my mean if Image Picker Lib will return current path it will return exactly file, when I pick photo with Image Picker it will create a scale image and new path to image scale in emulted, you can see function scale in https://github.com/react-community/react-native-image-picker/blob/develop/android/src/main/java/com/imagepicker/ImagePickerModule.java. and a problem in lib: method scale lib call in main thread so when we pick image have large size it will crash or lag – Trần Hòa May 18 '18 at 06:55