【问题标题】:Cannot save image from base64 into gallery无法将图像从 base64 保存到图库中
【发布时间】:2020-05-03 04:46:31
【问题描述】:
import {CameraPreview, CameraPreviewOptions,} from '@ionic-native/camera-preview';
import {ToastController} from "ionic-angular";
import {Base64ToGallery} from '@ionic-native/base64-to-gallery';

  private cameraPreviewOpts: CameraPreviewOptions = {
            x: 0,
            y: 0,
            width: window.screen.width,
            height: window.screen.height,
            camera: 'rear',
            tapPhoto: true,
            previewDrag: true,
            toBack: true,
            alpha: 1
  };

  constructor(
              public navCtrl: NavController,
              public toastCtrl: ToastController,
              public navParams: NavParams,
              private camera: CameraPreview,
              public platform: Platform,
              private base64ToGallery: Base64ToGallery
                 ) 
  {
         var browser = this.platform.is('core');
         if (!browser) {this.camera.startCamera(this.cameraPreviewOpts);}
  }

      takePicture() {
        this.camera.takePicture({}).then((base64Data) => {
          this.base64ToGallery.base64ToGallery(base64Data).then(
            res => alert('Saved image to gallery ' + JSON.stringify(res)),
            err => alert('Error saving image to gallery ' + JSON.stringify(err))
          );
        }, (err) => {
          alert(JSON.stringify(err));
        });
      }

当我尝试在图库中保存图片时,出现以下错误:

{"__zone_symbol__currentTask":{"type":"microTask","state":"notScheduled","source":"Promise.then","zone":"angular","cancelFn":null,"runCount":0}}

【问题讨论】:

    标签: ionic-framework ionic3


    【解决方案1】:

    1) 从 npmjs cordova plugin add cordova-plugin-camera-preview 安装 CameraPreview

    2) 将此代码放在@Component declare let CameraPreview; 之前

    3) 不要忘记在应用程序参数或使用 androidPermission 中允许存储访问和写入存储

    3) 有我的代码,它的工作!

    takePic(){
        const options = {
            x: 0,
            y: 0,
            width: window.screen.width,
            height: window.screen.height,
            camera: CameraPreview.CAMERA_DIRECTION.FRONT, //or BACK
            toBack: true,
            tapPhoto: false,
            tapFocus: false,
            previewDrag: false,
            disableExifHeaderStripping: true
        };
    
        CameraPreview.startCamera(options);
    
        let base64option : Base64ToGalleryOptions = {
            prefix: 'img',
            mediaScanner: false
        };
    
        CameraPreview.takePicture({}, base64PictureData => {
            let todecode = atob(base64PictureData);
    
            this.base64ToGallery.base64ToGallery(btoa(todecode), base64option).then(
                res => alert('Saved image to gallery '+ JSON.stringify(res)),
                err => alert('Error saving image to gallery ' + JSON.stringify(err))
              );
        }, error =>{
            alert(JSON.stringify(error));
        });
    }
    

    你必须在后台启动相机拍照toBack: true,解码和编码你的base64PictureDataatob & btoa

    您的图片现在在内部存储/图片中!

    【讨论】:

      【解决方案2】:

      我想如果你添加

      目的地类型:2

      图片会保存到本地,你会得到一个本地路径链接。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多