Tycholiz
Tycholiz

Reputation: 1190

How to mkdir with React-Native-FS

I'm trying to add functionality to my RN app that will allow users to create a new directory within their phone's file system.

I have tried to write the code so that the function creates a directory in the path /storage/emulated/0/AppName/NewFolder, since /storage/emulated/0 is the same path that is used by other apps I use to store user data (such as recording apps)

    makeDirectory = () => {
        const { currentFolder, units } = this.props;

        const directoryName = 'New Folder'
        const currentDirectory = units

        const absolutePath = `/storage/emulated/0/MyApp/${currentDirectory}`

        RNFS.mkdir(absolutePath)
            .then((result) => {
                console.log('result', result)
            })
            .catch((err) => {
                console.warn('err', err)
            })
    }

However, this is just giving me an error: directory could not be created. I feel I am missing something here, and am not supposed to be saving the files like this to the phone's system.

My ultimate goal is to have the app with it's own folder system that will be mirrored within /storage/emulated/0/MyApp/home

Upvotes: 4

Views: 13751

Answers (4)

Ali Raza Khan
Ali Raza Khan

Reputation: 138

I have resolved my issue by adding this line in the Android Manifest file

android:requestLegacyExternalStorage="true"

Upvotes: 0

Joaquim Dmt
Joaquim Dmt

Reputation: 87

You need to ask permission from the user for accessing his phone storage.

See this comment by Lylest on this reported issue from GitHub

Upvotes: 0

Muhammad Ashfaq
Muhammad Ashfaq

Reputation: 2511

I've just tried out and my folder is created successfully.

use

const absolutePath = `/storage/emulated/0/${currentDirectory}`

instead

const absolutePath = `/storage/emulated/0/MyApp/${currentDirectory}`

Upvotes: 1

sandeep rathod
sandeep rathod

Reputation: 105

 const    AppFolder    =     'DirNameyouwant';
 const DirectoryPath= RNFS.ExternalStorageDirectoryPath +'/'+ AppFolder;
 RNFS.mkdir(DirectoryPath);

Upvotes: 7

Related Questions