如何在 React Native (expo) 中创建文本文件?

hkg*_*ile 5 reactjs react-native expo

这是我正在尝试的功能。

saveFile = () => {
    let filename = Expo.FileSystem.documentDirectory + "text.txt";
    Expo.FileSystem.writeAsStringAsync(filename, "Hello World");
}

loadFile = () => {
    let filename = Expo.FileSystem.documentDirectory + "text.txt";
    let str = Expo.FileSystem.readAsStringAsync(filename, "Hello World");
    //alert(str);
}
Run Code Online (Sandbox Code Playgroud)

警告:

[Unhandled promise rejection: Error: Argument of an incompatible class: class java.lang.String cannot be passed as an argument to parameter expecting interface java.util.Map.]
Run Code Online (Sandbox Code Playgroud)

但是有一个警告,有人知道怎么做吗?

-------------更新:console.log 输出--------------

[15:27:36] Promise {
[15:27:36]   "_40": 0,
[15:27:36]   "_55": null,
[15:27:36]   "_65": 0,
[15:27:36]   "_72": null,
[15:27:36] }
Run Code Online (Sandbox Code Playgroud)

------------------更新:代码更新---------------

saveFile = async () => {
    let filename = Expo.FileSystem.documentDirectory + "text.txt";
    await FileSystem.writeAsStringAsync(filename, "Hello World", { encoding: FileSystem.EncodingTypes.UTF8 });
}

loadFile = async () => {
    let filename = Expo.FileSystem.documentDirectory + "text.txt";
    file = await FileSystem.readAsStringAsync(filename, { encoding: FileSystem.EncodingTypes.UTF8 });
    return file;
}

getTextFromFile = () => {
    value = this.loadFile();
    alert(value);
    console.log(value);
}
Run Code Online (Sandbox Code Playgroud)

你好世界似乎没有写入文件
------------更新-------------
我加了一行,text.txt文件出现在我的android模拟器中的Document/DCIM/text.txt,但是否必须使用“MediaLibrary.createAssetAsync”,如果是,如何控制文件夹名称?

await MediaLibrary.createAssetAsync(`${FileSystem.documentDirectory}text.txt`);
Run Code Online (Sandbox Code Playgroud)

anr*_*nro 8

我修改了问题中的代码并最终成功地完成了这项工作。此函数在下载文件夹中创建 .txt 文件:

import * as MediaLibrary from 'expo-media-library';
import * as FileSystem from 'expo-file-system';
import * as Permissions from 'expo-permissions';

saveFile = async () => {
    const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
    if (status === "granted") {
        let fileUri = FileSystem.documentDirectory + "text.txt";
        await FileSystem.writeAsStringAsync(fileUri, "Hello World", { encoding: FileSystem.EncodingType.UTF8 });
        const asset = await MediaLibrary.createAssetAsync(fileUri)
        await MediaLibrary.createAlbumAsync("Download", asset, false)
    }
}
Run Code Online (Sandbox Code Playgroud)