【发布时间】:2019-02-08 05:01:00
【问题描述】:
这是我正在尝试的功能。
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] }
------------------update: 代码更新---------------
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`);
【问题讨论】:
-
您似乎将一个额外的参数“Hello World”传递给了只需要文件名参数的
Expo.Filesystem.readAsStringAsync方法。我怀疑它会将您的“Hello World”字符串视为options对象,该对象定义应如何读取文件,如世博会文档中所述。 -
@iridescent 你是对的,但如何提醒字符串?它向我展示了 [object object]
标签: reactjs react-native expo