ReactNative Android 不会将文件保存到外部存储

问题描述

我正在使用 ReactNative 构建一个 Android 应用程序。我的应用程序需要将文件写入外部存储。我正在使用这个库,https://www.npmjs.com/package/react-native-fs。这就是我迄今为止所做的。

我在 AndroidManifest.xml 文件中有以下权限

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

然后我请求许可如下

const requestPermissions = async () => {
        console.log("Requesting the permissions")
        let granted = await PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,{
                'title': 'Write File Permission','message': 'The app needs the permission to save the file to the device storage.'
            }
        )

        if (granted ==  PermissionsAndroid.RESULTS.GRANTED) {
            Alert.alert("Permission","Request has been granted")
        } else {
            Alert.alert("Permission","Request was not granted")
        }
    }

然后我按如下方式导入库。

var RNFS = require('react-native-fs');
var path = RNFS.DocumentDirectoryPath + "/test.txt"

然后我将文件保存如下

const saveAsFile = () => {
        RNFS.writeFile(path,message,'utf8')
            .then(success => {
                Alert.alert("Save","File has been saved")
            })
            .catch(error => {
                console.log(error)
                Alert.alert("Save","Unable to save the file")
            })
    }

它没有抛出错误。成功回调被调用。但是我在设备的 Documents 文件夹中看不到该文件。我的代码有什么问题?为什么没有在设备上创建文件

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)