这是我正在尝试的函数。
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.]
但是有一个警告,有人知道该怎么做吗?
-------------更新:控制台.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);
}
你好世界似乎没有写入文件
------------更新-------------
我添加了一行,文本.txt文件出现在我的 android 模拟器中的文档/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)
}
}