본문 바로가기
Mobile

[ IONIC ] - FIle Read 에러 {"code":2,"message":"SECURITY_ERR"}

by 기저귀찬개발자 2020. 8. 15.

카메라에서 사진을 찍거나 갤러리에서 선택한 사진을 파이어베이스에 업로드 하던 도중에 생긴 오류이다.

 

    const options: CameraOptions = {
      quality: 100,
      sourceType: sourceType,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    }

    this.camera.getPicture(options).then((imageData) => {
      this.file
        .resolveLocalFilesystemUrl(imagePath)
        .then((fileEntry) => {
          let { name, nativeURL } = fileEntry;
          let path = nativeURL.substring(0, nativeURL.lastIndexOf("/"));
  
  	      //아래 줄에서 Buffer를 생성하려 할때 생긴 문제였다.
          return this.file.readAsArrayBuffer(path, name);

        })
        .then(buffer => {
          console.log('buffer')
          let imgBlob = new Blob([buffer], {
            type: "image/jpeg"
          });
          resolve(
            imgBlob
          );
        });
        
    }).catch(err=>console.log(err));

 

이전에는 잘되다가 targetSdkVersion를 29로 올리니 아래와 같이 에러가 발생했다.

core.js:6014 ERROR Error: Uncaught (in promise): FileError: {"code":2,"message":"SECURITY_ERR"}

 

해결책으로는 FilePath 패키지를 사용하여 content://로 접근하던 경로를 file://로 변환하여 접근하니 잘 되었다.

   const options: CameraOptions = {
      quality: 100,
      sourceType: sourceType,
      destinationType: this.camera.DestinationType.FILE_URI,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    }

    this.camera.getPicture(options).then((imageData) => {
 
    this.filePath.resolveNativePath(imageData).then((data)=>{
      let path = data.substring(0, data.lastIndexOf("/"));
      let name = data.substring(data.lastIndexOf("/")+1);

      return this.file.readAsArrayBuffer(path, name);

    }).then(buffer => {
        // get the buffer and make a blob to be saved
        let imgBlob = new Blob([buffer], {
          type: "image/jpeg"
        });

        resolve(
          imgBlob
        );
      });
    }).catch(err=>console.log(err));

 

댓글