gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:49:33 24 4
gpt4 key购买 nike

这是我正在尝试的功能。

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

警告:

[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.]

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

-------------更新: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] }

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

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

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

await MediaLibrary.createAssetAsync(`${FileSystem.documentDirectory}text.txt`);

最佳答案

我修改了问题中的代码,终于成功了。此函数在下载文件夹中创建 .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)
}
}

关于reactjs - 如何在 React Native(expo)中创建文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54586216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com