无法指定图片上传路径-ionic 3

时间:2018-09-05 08:24:17

标签: typescript cordova ionic-framework ionic3 phonegap

在通过图像选择器裁剪后,我试图使用ionic应用程序将图像上传到php服务器。图片在显示的路径中不存在。

有什么方法可以指定保存裁切图像的路径吗?

这是我的代码:

getImage()
{
  var options = {
   maximumImagesCount: 1,
   width: 800,
   height: 800,
   quality: 80,
  };

 this.imagePicker.getPictures(options)
 .then((results) => {
   this.reduceImages(results).then(() => {
        this.photos = results;

        const fileTransfer: FileTransferObject = this.transfer.create();

            let d = new Date();
            let time = d.getTime();
            let options_f: FileUploadOptions = {
              fileKey: 'file',
              fileName: this.name + time + '.jpg',
              chunkedMode: false,
              mimeType: "image/jpg",
            };

            let url='https://elevather.com/mentor/file_upload.php';

            fileTransfer.upload(this.photos, url, options_f)
            .then((data) => {
              if(data["_body"]=="yes")
                this.msg = 'yes';
              else
                this.msg = 'no';
            }, (err) => {
              console.log('upload failed!');
            });

    }, (err) => { });
  }, (err) => { });
}

reduceImages(selected_pictures: any) : any{
  return selected_pictures.reduce((promise:any, item:any) => {
    return promise.then((result) => {
      return this.cropService.crop(item, {quality: 80})
       .then(cropped_image => console.log('Done'));
     });
  }, Promise.resolve());
}

还有其他方法吗?

2 个答案:

答案 0 :(得分:0)

您尝试访问此路径吗?这是保存它的地方(https://github.com/jeduan/cordova-plugin-crop/blob/master/src/android/CropPlugin.java)。

 private String getTempDirectoryPath() {
    File cache = null;

    // SD Card Mounted
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        cache = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                "/Android/data/" + cordova.getActivity().getPackageName() + "/cache/");
    }
    // Use internal storage
    else {
        cache = cordova.getActivity().getCacheDir();
    }

    // Create the cache directory if it doesn't exist
    cache.mkdirs();
    return cache.getAbsolutePath();
}

答案 1 :(得分:0)

您是否已使用croppedImage替换了this.photos?请参见下面的代码。

getImage()
{
  var options = {
   maximumImagesCount: 1,
   width: 800,
   height: 800,
   quality: 80,
  };

 this.imagePicker.getPictures(options)
 .then((results) => {
   this.reduceImages(results).then((croppedImage) => {
        this.photos = croppedImage;

        const fileTransfer: FileTransferObject = this.transfer.create();

            let d = new Date();
            let time = d.getTime();
            let options_f: FileUploadOptions = {
              fileKey: 'file',
              fileName: this.name + time + '.jpg',
              chunkedMode: false,
              mimeType: "image/jpg",
            };

            let url='https://elevather.com/mentor/file_upload.php';

            fileTransfer.upload(this.photos, url, options_f)
            .then((data) => {
              if(data["_body"]=="yes")
                this.msg = 'yes';
              else
                this.msg = 'no';
            }, (err) => {
              console.log('upload failed!');
            });

    }, (err) => { });
  }, (err) => { });
}

reduceImages(selected_pictures: any) : any{
  return selected_pictures.reduce((promise:any, item:any) => {
    return promise.then((result) => {
      return this.cropService.crop(item, {quality: 80})
       .then(cropped_image => cropped_image);
     });
  }, Promise.resolve());
}