joltup / rn-fetch-blob

A project committed to making file access and data transfer easier, efficient for React Native developers.
MIT License
2.81k stars 770 forks source link

RNFetchBlob.fs.stat() fails when passed a directory content URI #787

Open maciejewiczow opened 2 years ago

maciejewiczow commented 2 years ago

Hi ! Thank you for reporting an issue, but we would like to remind you, we have a trouble shooting page in our wiki. You may want to take a look on that page or find issues tagged "trouble shooting" :p (done)

I am trying to stat a directory by its content uri to get its real path. Stat call fails with failed to stat path "null" because it does not exist or it is not a folder. The URI looks like this content://com.android.externalstorage.documents/tree/primary:Movies/VLC.

My code looks like this:

const opts: DocumentPickerOptions<SupportedPlatforms> = {
    presentationStyle: 'fullScreen',
    type: 'application/octet-stream',
};

try {
    if (Platform.OS === 'android') {
        const res = await PermissionsAndroid.requestMultiple([
            PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
            PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
        ]);

        if (Object.values(res).find(status => status !== 'granted')) {
            console.log('permission denied', res);
            return;
        }
    }

    const directory = await pickDirectory(opts);

    if (!directory)
        return;

    directory.uri = decodeURIComponent(directory.uri);

    console.log(directory);

    await RNFetchBlob.fs.stat(directory.uri);
} catch (e) {
    console.log(e)
}

The pickDirectory is a function from react-native-document-picker

I've added needed permission to the AndroidManifest file and also enabled android:requestLegacyExternalStorage="true".

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Please help, as I do not know any other way to get a directory real path. The stat call completes successfully when content URI points at a file.

rohitGalaxy commented 1 year ago

did you find any solution for this. Please paste here.....

maciejewiczow commented 1 year ago

I did not find any solution for that

fukemy commented 1 year ago

i found if url have space then this problem appear

sabarivelanganesan commented 12 months ago

I got the same issue. please let me know if you find any solution over this please

BRTZL commented 9 months ago

i found if url have space then this problem appear

Please pass the decoded file URI as demonstrated below to resolve this issue.

const fileStat = await RNFetchBlob.fs.stat(decodeURI(fileUri));