Persistent file store - is this path correct?


#1

Hi, I am saving a camera photo to my app’s data directory. It is necessary that the image is not deleted unless the app is deleted (that is, it persists cache cleans and any garbage collection done by the OS)

The path that is generated, is this: file:///var/mobile/Containers/Data/Application/1239C7A4-0564-4FDB-B72B-32477C6ADAC2/tmp/cdv_photo_011.jpg

I’m concerned by the “tmp” directory that is part of the path that is generated by cordova.file.dataDirectory . Can anyone help confirm that this directory won’t be removed/or if my code is correct: (The code is a mostly direct copy from this article)

// Get the data of an image
    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());
          });
      } else {
        var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
        var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
        this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
      }
      
      console.log('Save to:' + correctPath + currentName);
      this.selectedCategory.addItem({ notes: 'test', photo: correctPath + currentName });

    }, (err) => {
      this.presentToast('Error while selecting image.');
    });
  }


  // Create a new name for the image
  private createFileName() {
    var d = new Date(),
      n = d.getTime(),
      newFileName = n + ".jpg";
    return newFileName;
  }

  // Copy the image to a local folder
  private copyFileToLocalDir(namePath, currentName, newFileName) {
    this.file.copyFile(namePath, currentName, cordova.file.dataDirectory, newFileName).then(success => {
      this.lastImage = newFileName;
    }, error => {
      this.presentToast('Error while storing file.');
    });
  }

#2

Whoops problem solved - it was my programming error - that “tmp” directory was not the datadirectory. I was storing and restoring the temporary file path generated by getpicture. I also realized I was storing the literal value of datadirectory which is wrong as it will change from time to time.