《鸿蒙开发-答案之书》将系统路径文件复制到沙盒工具类

100 阅读1分钟
《鸿蒙开发-答案之书》将系统路径文件复制到沙盒工具类

如果没有问存储权限,是不能访问系统文件的。但是可以把系统文件拷贝到你app的沙盒里面,就可以访问了。沙盒本质就是你app缓存的目录

直接上代码:

  public static async copyToSandbox(uris: Array<string>) : Promise<Array<string>> {
    let targetList = new Array<string>();
    if (!ArrayChecker.isValid(uris)) {
      return targetList;
    }
    for (let u of uris) {
      let file = await fs.open(u, fs.OpenMode.READ_ONLY);
      let targetPath = getContext().cacheDir + "/" + file.name;
      fs.closeSync(file);
      targetPath = "file://" + targetPath;
      await fs.copy(u, targetPath);
      targetList.push(targetPath);
    }
    return targetList;
  }