【问题标题】:How to mkdir with React-Native-FS如何使用 React-Native-FS mkdir
【发布时间】:2019-03-29 00:12:52
【问题描述】:

我正在尝试向我的 RN 应用程序添加功能,允许用户在手机的文件系统中创建新目录。

我尝试编写代码,以便函数在路径 /storage/emulated/0/AppName/NewFolder 中创建一个目录,因为 /storage/emulated/0 与我使用的其他应用程序使用的路径相同存储用户数据(例如录音应用)

    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)
            })
    }

但是,这只是给我一个错误:无法创建目录。我觉得我在这里遗漏了一些东西,不应该将这样的文件保存到手机系统中。

我的最终目标是让应用程序拥有自己的文件夹系统,该文件夹系统将在 /storage/emulated/0/MyApp/home 中进行镜像

【问题讨论】:

    标签: react-native react-native-fs


    【解决方案1】:
     const    AppFolder    =     'DirNameyouwant';
     const DirectoryPath= RNFS.ExternalStorageDirectoryPath +'/'+ AppFolder;
     RNFS.mkdir(DirectoryPath);
    

    【讨论】:

    • 虽然您的代码可能会回答问题,但包含解释通常很有用。
    【解决方案2】:

    我刚刚试了一下,我的文件夹创建成功了。

    使用

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

    改为

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

    【讨论】:

    • @SecretAgentMan,在 android 11 中,我无法创建具有权限和其他配置的导演。你有没有在android 11的外部存储中创建目录???
    【解决方案3】:

    您需要征得用户的许可才能访问他的手机存储空间。

    查看Lylestthis GitHub 报告问题的评论

    【讨论】:

      【解决方案4】:

      我已通过在 Android 清单中添加这一行来解决我的问题 文件

      android:requestLegacyExternalStorage="true"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多