I can't open a file in /data #240
-
I just need to read the file test.txt , but when I try to get a file or the contents of a folder, I always get undefined. What I'm doing wrong :( |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 14 replies
-
Refer to the hierarchy on this doc: https://docs-testing.zepp.com/docs/guides/framework/device/fs/ It looks to me that your data folder is stored outside the assets folder. In the future please make sure you attach a reproducible project. It can get very tricky to debug a screenshot. @mimimix |
Beta Was this translation helpful? Give feedback.
-
I see now. Here's a working example for you to play with: import { openAssetsSync, readSync, } from "@zos/fs";
const FILE_PATH = "data/test.txt";
function bufferToString(buffer){
return String.fromCharCode.apply(null, new Uint8Array(buffer));
}
Page({
build() {
const fd = openAssetsSync({
path: FILE_PATH,
flag: O_RDONLY,
});
const buffer = new ArrayBuffer(1024);
const result = readSync({
fd,
buffer,
});
if (result > 0) {
console.log("readSync succeed");
console.log(bufferToString(buffer));
} else {
console.log("readSync failed", result);
}
},
}); Your hierarchy for this project should be: |
Beta Was this translation helpful? Give feedback.
-
I also had problems with reading files. |
Beta Was this translation helpful? Give feedback.
-
@silver-zepp This only partially solves the problem. While I'm working with one version of the program, this method works. But as soon as I update the program, the saved data will still disappear. It would not be bad to add such a feature to the description of the operation of these functions when working on a simulator. I spent a lot of time thinking that I was not storing data correctly and trying to solve this problem. |
Beta Was this translation helpful? Give feedback.
-
@silver-zepp I am trying to communicate with the phone via an http connection to 127.0.0.1. The watchface uses a miniapp only as a bridge. The miniapp writes the http results to a file that the watchface then loads. Can you recommend an alternative method for getting data from the phone to the watchface? Many thanks for your help |
Beta Was this translation helpful? Give feedback.
I see now. Here's a working example for you to play with:
Your hierarchy for this project should be:
assets/390x450-g…