不允许加载本地资源:ionic 3 android

Mun*_*han 11 android file path android-6.0-marshmallow ionic3

我正在使用ionic 3 android build apk并尝试从文件中添加图像:///storage/emulated/0/data/io.ionic.vdeovalet/cache/image.jpeg




    takePicture(sourceType) {
        try {
    // Create options for the Camera Dialog
          var options = {
            quality: 100,
            destinationType: this.camera.DestinationType.FILE_URI,
            encodingType: this.camera.EncodingType.JPEG,
            sourceType: sourceType,
          };
          this.camera.getPicture(options).then((imagePath) => {
    // Special handling for Android library
            if (this.platform.is('android') && sourceType ===
              this.camera.PictureSourceType.PHOTOLIBRARY) {
              this.filePath.resolveNativePath(imagePath)
                .then(filePath => {
                  let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
                  let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1,
                    imagePath.lastIndexOf('?'));
                  this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
                  this.lastImage = filePath;
                });
            } else {
              var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
              var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
              this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
            }
          }, (err) => {
            this.presentToast('Error while selecting image.');
          });


        } catch (e) {
          console.error(e);
        }


      }

错误:不允许加载本地资源
android 6.0.1

小智 15

我遇到了同样的问题,结果证明新的离子webview插件是导致问题的原因.

新插件:cordova-plugin-ionic-webview @ 2.x似乎不稳定......

让它降级回到cordova-plugin-ionic-webview@1.2.1并且一切都应该工作

脚步:

1.卸载webview

ionic cordova plugins rm cordova-plugin-ionic-webview
Run Code Online (Sandbox Code Playgroud)

2.安装旧的:

ionic cordova plugins add cordova-plugin-ionic-webview@1.2.1
Run Code Online (Sandbox Code Playgroud)

3.清洁cordova

cordova clean android
Run Code Online (Sandbox Code Playgroud)


kun*_*wat 7

无需降级,只需编写此代码。

private win: any = window;
    this.win.Ionic.WebView.convertFileSrc(path);
Run Code Online (Sandbox Code Playgroud)

  • ..把这段代码写在哪里?你应该理性地解释你的代码。 (5认同)

小智 5

当 Ionic 与 Capacitor 一起使用时,我们可以通过以下方式在本机设备上获取图像或其他资源的正确路径:

import { Capacitor } from '@capacitor/core';

Capacitor.convertFileSrc(filePath); 
Run Code Online (Sandbox Code Playgroud)

https://ionicframework.com/docs/core-concepts/webview


小智 0

尝试这个:

const options: CameraOptions = {
    quality: 10
    , destinationType: this.camera.DestinationType.DATA_URL
    , mediaType: this.camera.MediaType.PICTURE
    // Optional , correctOrientation: true
    , sourceType: sourceType == 0 ? this.camera.PictureSourceType.CAMERA : this.camera.PictureSourceType.PHOTOLIBRARY
    // Optional , saveToPhotoAlbum: true
};

this.camera.getPicture(options).then(imageBase64 => {
    let txtForImage = `data:image/jpeg;base64,` + imageBase64;
    this.imageToLoad = txtForImage;
})
.catch(error => {
    alert("Error: " + error);
    console.error(error);
});
Run Code Online (Sandbox Code Playgroud)