Search code examples
androidreact-nativern-fetch-blob

How to open File Manager and pick a directory before downloading file in React Native


I'm developing a React Native app for Android platform and I have to download file by using rn-fetch-blob library. I find out some apps can open File Manager of Android and choose a directory when user download. So how can I do that?

My app download using writeFile method because it's a base64, not using fetch file from url.

Thanks for reading question and have a nice day!


Solution

  • You can use this package react-native-scoped-storage

    import * as ScopedStorage from "react-native-scoped-storage"
    
    let dir = await ScopedStorage.openDocumentTree(true);
    

    Once the user selects a directory, we will recieve the information about the directory and its uri. Now we can use this uri to read/write.

    // We can store this directory in AsyncStorage for later use.
    await AsyncStorage.setItem('userMediaDirectory',JSON.stringify(dir));