Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ionic framework readAsDataUrl函数没有输出_Ionic Framework_Cordova Plugins_Capacitor - Fatal编程技术网

Ionic framework readAsDataUrl函数没有输出

Ionic framework readAsDataUrl函数没有输出,ionic-framework,cordova-plugins,capacitor,Ionic Framework,Cordova Plugins,Capacitor,我用的是离子5和电容器。 出于某种原因,readAsDataURL不工作,也没有向我显示错误消息。 路径和文件名看起来不错,它们是: 文件路径:file:///storage/emulated/0/Android/data/io.ionic.starter/cache/1763816379-cropped.jpg showCroppedImage(ImagePath) { var filePath = ImagePath; let fileName = filePath.spl

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

文件路径: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);
      });
  }
路径: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);
      });
  }
早些时候,我的文件被保存在一个用户无法访问的内部目录中。所以我使用了externalDataDirectory函数,它解决了我的问题。它将文件存储在用户可以访问的外部目录中。 该文件存储在“内部存储/android/data/#app#u id#/”中

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

我最后为我的应用程序排序了问题,并将其包括在下面:

/** @desc crop the selected image */
async cropImage(fileUrl: any) {
    this.show_spinner = true;
    await this.ionLoader.showLoader('Cropping Image...');
    this.crop.crop(fileUrl, {
      quality: 75,
      targetHeight: 320,
      targetWidth: 240
    })
      .then(
        async (newImage) => {
          this.show_spinner = false;
          await this.ionLoader.hideLoader();
          this.showCroppedImage(newImage.split('?')[0]);
        },
        async (error: IonicResultMessageInterface) => {
          this.show_spinner = false;
          await this.ionLoader.hideLoader();
          console.error('Error cropping image', error);
          console.log(error);
          this.setUserProfileImage();
          this.toasterService.showToast(error.message, 'failure');
        }
      );
  }
  /** @desc show the image after it has been cropped */
  async showCroppedImage(ImagePath: string) {
    console.log(ImagePath);
    this.show_spinner = true;
    const copyPath = ImagePath;
    const splitPath = copyPath.split('/');
    const imageName = splitPath[splitPath.length - 1];
    const file_ext = imageName.substr(imageName.lastIndexOf('.') + 1);
    try {
      const base64 = await Filesystem.readFile({ path: ImagePath});
      if (base64) {
        console.log(base64);
        this.userImageUrl = 'data:image/' + file_ext + ';base64,' + base64.data;
        this.show_spinner = false;
        await this.updateProfilePictureMethod();
      } else {
        this.show_spinner = false;
        console.log('Error in the showCroppedImage File method');
        console.log('Unexpected Error');
        this.setUserProfileImage();
        this.toasterService.showToast('Unexpected Error Occurred in the showCroppedImage File', 'failure');
      }
    } catch (error) {
      this.show_spinner = false;
      console.log('Error in the showCroppedImage File method');
      console.log('Unexpected Error');
      this.setUserProfileImage();
      this.toasterService.showToast(error.message, 'failure');
    }
  }

最后,我为我的申请整理了问题,并将其包括在下面:

/** @desc crop the selected image */
async cropImage(fileUrl: any) {
    this.show_spinner = true;
    await this.ionLoader.showLoader('Cropping Image...');
    this.crop.crop(fileUrl, {
      quality: 75,
      targetHeight: 320,
      targetWidth: 240
    })
      .then(
        async (newImage) => {
          this.show_spinner = false;
          await this.ionLoader.hideLoader();
          this.showCroppedImage(newImage.split('?')[0]);
        },
        async (error: IonicResultMessageInterface) => {
          this.show_spinner = false;
          await this.ionLoader.hideLoader();
          console.error('Error cropping image', error);
          console.log(error);
          this.setUserProfileImage();
          this.toasterService.showToast(error.message, 'failure');
        }
      );
  }
  /** @desc show the image after it has been cropped */
  async showCroppedImage(ImagePath: string) {
    console.log(ImagePath);
    this.show_spinner = true;
    const copyPath = ImagePath;
    const splitPath = copyPath.split('/');
    const imageName = splitPath[splitPath.length - 1];
    const file_ext = imageName.substr(imageName.lastIndexOf('.') + 1);
    try {
      const base64 = await Filesystem.readFile({ path: ImagePath});
      if (base64) {
        console.log(base64);
        this.userImageUrl = 'data:image/' + file_ext + ';base64,' + base64.data;
        this.show_spinner = false;
        await this.updateProfilePictureMethod();
      } else {
        this.show_spinner = false;
        console.log('Error in the showCroppedImage File method');
        console.log('Unexpected Error');
        this.setUserProfileImage();
        this.toasterService.showToast('Unexpected Error Occurred in the showCroppedImage File', 'failure');
      }
    } catch (error) {
      this.show_spinner = false;
      console.log('Error in the showCroppedImage File method');
      console.log('Unexpected Error');
      this.setUserProfileImage();
      this.toasterService.showToast(error.message, 'failure');
    }
  }

你能解决这个问题吗?我也遇到同样的问题。我得到一个404错误,经过一点调试,它是由于“file:///storage/emulated/0/An“不允许访问。这只是crop访问文件的唯一方法,因为网页不起作用。Base64也不起作用,因为它甚至不能正常工作。我实际上不记得我做了什么,给我一两天时间,我会弄清楚的,然后再给你回复。同时,如果您愿意,这里是我使用的代码:好的,我想我已经找到了,如果可能的话,请让我看看您的代码好吗?@fromage9747我在下面添加了一个解决方案,请查看!请让我知道它是否有效:)你能解决这个问题吗?我也遇到同样的问题。我得到一个404错误,经过一点调试,它是由于“file:///storage/emulated/0/An“不允许访问。这只是crop访问文件的唯一方法,因为网页不起作用。Base64也不起作用,因为它甚至不能正常工作。我实际上不记得我做了什么,给我一两天时间,我会弄清楚的,然后再给你回复。同时,如果您愿意,这里是我使用的代码:好的,我想我已经找到了,如果可能的话,请让我看看您的代码好吗?@fromage9747我在下面添加了一个解决方案,请查看!请让我知道它是否有效:)我应该回到这一点,并提出我自己的答案。在这篇评论中添加了一条评论后不久,我想出了一个方法来做我想做的事情。我最终使用了const base64=await Filesystem.readFile({path:ImagePath});我应该回到这个问题上来,提出我自己的答案。在这篇评论中添加了一条评论后不久,我想出了一个方法来做我想做的事情。我最终使用了const base64=await Filesystem.readFile({path:ImagePath});