Переместить файл из временной папки во внешний каталог, не работающий на устройстве Android 8 и выше - PullRequest
0 голосов
/ 29 июня 2019

Я использую плагин IonicMultiCamera, плагин CameraPreview и 'cordova-file-plugin' для захвата и перемещения изображений из кэша в указанную папку в externalRootDirectory.Этот процесс хорошо работает в Android версии 7 и ниже, но не работает в Android версии 8 и выше.

Для Android версии 8 и выше функция 'moveFile' 'cordova-file-plugin'выдает ошибку как: {code: 1, message: 'NOT_FOUND_ERR'}.

/*--------FUNCTION TO CAPTURE IMAGE-------*/
    capturepic()
      {
        this.cachearray=[];

          console.log('capturepic');
          const pictureOptions: CameraPreviewPictureOptions = {
            quality: 80,
            width: 640,
            height: 640
          };
          const translations: CameraTranslations = {
            cancel: 'Cancel',
            finish: 'Finish',
            auto: 'AUTO',
            on: 'On',
            off: 'Off'
          };
          this.camera.getPicture(pictureOptions)
          .then((pictures: Array<Picture>) => {
            console.log("success in camera"+JSON.stringify(pictures));
            for(let i=0;i<Object.keys(pictures).length;i++)
            {
              console.log('picturesarray'+JSON.stringify(pictures));

            console.log(" getPicture fileEntry "+pictures[i].fileEntry.name)

            let imagePath=pictures[i].fileEntry.nativeURL;
            let imagename=pictures[i].fileEntry.name;
            let  imagePatho=imagePath.substr(0,imagePath.lastIndexOf('/')+1);
            let fullpath=imagePath+imagename;

            let filepathnew="file:///storage/emulated/0/Customervisitingcard/";

            console.log(" imagePatho fileEntry "+imagePatho);
              this.cachearray.push({imagePath:imagePath, imagename:imagename});
              console.log(" cachearray "+JSON.stringify(this.cachearray));
              console.log("iiiiii "+i+" llllll "+(Object.keys(pictures).length-1));
              if(i==Object.keys(pictures).length-1)
              {
                console.log("iffff ");
                this.copyonclick();
              }
           }
          })
          .catch(err => {
            console.log("Error in camera"+err);
          });
       // }
      }

    /*--------FUNCTION TO COPY IMAGES-------*/
      copyonclick(){
        console.log("copyonclick"+Object.keys(this.cachearray).length+JSON.stringify(this.cachearray));

        for(let i=0;i<Object.keys(this.cachearray).length;i++)
        {

        let imagePath=this.cachearray[i].imagePath;
        let imagename=this.cachearray[i].imagename;

        let fullpath=imagePath+imagename;
        // let suppliername=this.supplier_name;
        // let productname=this.input;
        // let offerno=this.offernumber;
        let extrtdir = this.file.externalRootDirectory;
        console.log('extrtdir=='+extrtdir);
        let filepathnew= this.file.externalRootDirectory+'Customervisitingcard/'; 
        console.log('imagePath'+imagePath+'imagename'+imagename);

         this.resolvefile(imagePath,imagename,filepathnew);



        }
      }

      resolvefile(imagePath,imagename,filepathnew)
      {
        this.filePath.resolveNativePath(imagePath)
        .then(filePath => {
          let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
          let currentName = imagename;
          console.log("resolveNativePath "+correctPath+" currentName " + currentName +'filepathnew'+filepathnew);
          this.copyFileToLocalDir(correctPath, currentName, this.createFileName(),filepathnew);

        });

      }

      private createFileName() {
        console.log("createFileName");
        var d = new Date(),
        n = d.getTime(),
        newFileName =  n + ".jpg";
        return newFileName;
      }

     /*--------FUNCTION TO MOVE IMAGES TO SPECIFIED EXTERNAL FOLDER-------*/
      private copyFileToLocalDir(namePath, currentName, newFileName,filepathnew) {
        this.file.moveFile(namePath, currentName, filepathnew, newFileName).then(success => {

          this.storedfilepathname = filepathnew+newFileName;
          this.storeimages.push(this.storedfilepathname);
          console.log('storeimages'+JSON.stringify(this.storeimages)+'uuuuuuu'+this.storedfilepathname);

          this.file.readAsDataURL(filepathnew, newFileName).then(dataurl => {
            this.lastImage = dataurl;
            this.finalimages.push({src:this.lastImage});
            console.log(" finalimages "+JSON.stringify(this.finalimages));
          },
        (error) =>{
          console.log("readAsDataURL errr "+error.message);
        });

          console.log("copyFileToLocalDir"+newFileName);
        }, error => {
          console.log('storeerror='+JSON.stringify(error));
          this.presentToast('Error while storing file.');
        });
      }

Путь к каталогу, куда я хочу скопировать изображения, может быть "this.file.externalRootDirectory + 'Customervisitingcard / '";

...