这是我正在尝试的功能。
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);
}
您好世界似乎没有写入文件
------------更新-------------
我添加了一行,text.txt文件出现在我的Android仿真器的Document / DCIM / text.txt中,但是必须使用“ MediaLibrary.createAssetAsync”,如果是的话,如何控制文件夹名称? / p>
await MediaLibrary.createAssetAsync(`${FileSystem.documentDirectory}text.txt`);
答案 0 :(得分:0)
我已经修改了问题中的代码,最后设法完成了这项工作。此功能在Downloads文件夹中创建.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)
}
}