Not allowed to load local resource: ionic 3 android

后端 未结 9 1600

I am using ionic 3 android build apk and trying to laod image from file:///storage/emulated/0/data/io.ionic.vdeovalet/cache/image.jpeg


         


        
9条回答
  •  佛祖请我去吃肉
    2020-12-14 20:27

    Copy this line into your index.html

    
    

    Then, write this function instead of your one, note that what this script does is returning the photo as base64

    getImageFromCamera() {
    const options: CameraOptions = {
        quality: 20,
        saveToPhotoAlbum: true,
        destinationType: this.camera.DestinationType.FILE_URI,
        sourceType: this.camera.PictureSourceType.CAMERA,
        encodingType: this.camera.EncodingType.JPEG,
        allowEdit: false
    };
    this.camera.getPicture(options).then((imageData) => {
        this.imageURI = imageData;
        this.imageName = imageData.substr(imageData.lastIndexOf('/') + 1);
        // Create a folder in memory location
        this.file.checkDir(this.file.externalRootDirectory, 'Demo')
            .then(() => {
                this.fileCreated = true;
            }, (err) => {
                console.log("checkDir: Error");
                this.presentToast("checkDir Failed");
            });
        if (this.fileCreated) {
            this.presentToast("Directory Already exist");
        }
        else {
            this.file.createDir(this.file.externalRootDirectory, "Demo", true)
                .then((res) => {
                    this.presentToast("Directory Created");
                }, (err) => {
                    console.log("Directory Creation Error:");
                });
        }
    
        //FILE MOVE CODE
        let tempPath = this.imageURI.substr(0, this.imageURI.lastIndexOf('/') + 1);
        let androidPath = this.file.externalRootDirectory + '/Bexel/';
        this.imageString = androidPath + this.imageName;
    
        this.file.moveFile(tempPath, this.imageName, androidPath, this.imageName)
            .then((res) => {
                this.presentToast("Image Saved Successfully");
                this.readImage(this.imageString);
    
            }, (err) => {
                console.log("Image Copy Failed");
                this.presentToast("Image Copy Failed");
            });
        //Complete File Move Code
    
        this.toDataURL(this.imageURI, function (dataUrl) {
            console.log('RESULT:' + dataUrl);
        });
      }, (err) => {
        console.log(JSON.stringify(err));
        this.presentToast(JSON.stringify(err));
      });
    }
    presentToast(msg) {
    let toast = this.toastCtrl.create({
        message: msg,
        duration: 2000
    });
    toast.present();
    }
    toDataURL(url, callback) {
    let xhr = new XMLHttpRequest();
    xhr.onload = function () {
        let reader = new FileReader();
        reader.onloadend = function () {
            callback(reader.result);
        };
        reader.readAsDataURL(xhr.response);
    };
    xhr.open('GET', url);
    xhr.responseType = 'blob';
    xhr.send();
    }
    readImage(filePath) {
    let tempPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
    let imageName = filePath.substr(filePath.lastIndexOf('/') + 1);
    
    this.file.readAsDataURL(tempPath, imageName)
        .then((res) => {
            this.presentToast("Image Get Done");
            this.imageUrl = res;
        }, (err) => {
            this.presentToast("Image Get Error");
        });
    }
    

    It sees like it's an issue with content CSP (content security policy), the meta tag should fix this issue, then the code will read the photo as base64, then here you go, in HTML:

    
    

    And you can modify the function to remove unnecessary console.log, i was just testing.

提交回复
热议问题