首页 > 解决方案 > readAsDataUrl 函数没有输出

问题描述

我正在使用离子 5 和电容器。出于某种原因,readAsDataURL 不起作用,也没有向我显示错误消息。路径和文件名看起来不错,它们是:

文件路径:file:///storage/emulated/0/Android/data/io.ionic.starter/cache/1763816379-cropped.jpg

路径:file:///storage/emulated/0/Android/data/io.ionic.starter/cache/

文件名:1763816379-cropped.jpg

 showCroppedImage(ImagePath) {
    var filePath = ImagePath;
    let fileName = filePath.split("/").pop();
    let path = filePath.substring(0, filePath.lastIndexOf("/") + 1);
    alert(filePath);
    alert(fileName);
    alert(path);
    alert("works till here");
    this.file
      .readAsDataURL(path, fileName)
      .then((base64) => {
        alert(base64);
      })
      .catch((err) => {
        console.log(err);
        alert(err);
      });
  }

标签: ionic-frameworkcordova-pluginscapacitor

解决方案


  this.file.createFile(
    this.file.externalDataDirectory,
    "base64string.txt",
    true
  );
  this.file.readAsText(this.file.externalDataDirectory, "base64string.txt");
  this.blob = new Blob([this.base64string]);
  this.file
    .writeFile(
      this.file.externalDataDirectory,
      "base64string.txt",
      this.blob,
      { replace: true, append: false }
    )
    .then(() => {
      alert("File saved in internal storage/android/data/io.starter.ionic/");
});
}

早些时候,我的文件被保存在用户无法访问的内部目录中。所以我使用了 externalDataDirectory 函数,它解决了我的问题。它将文件存储在用户可以访问的外部目录中。该文件存储在“内部存储/android/data/#app_id#/”中


推荐阅读