为什么在更改页面时我的文件被删除了?

时间:2017-12-19 10:21:12

标签: android file ionic-framework

我有一个离子应用程序,它会拍照并将其保存到文件中。一切似乎都有效,除了某些原因,一旦我改变页面,文件就不再存在了。

拍摄照片后会写下以下代码,并确认目录存在

writePhotoToFile(dir: string, data: string) {
  this.file.writeFile(dir, 'profile-photo.jpg', data, {replace: true})
  .then(_ => {
    console.log(dir + 'profile-photo.jpg written');
    this.file.checkFile(dir, 'profile-photo.jpg')
    .then(_ => {
      console.log('Confirmed. File was created');
      this.file.readAsText(dir, 'profile-photo.jpg')
      .then(photo_text => {
        //console.log(photo_text);
        this.navCtrl.push(ProfilePage);
      })
      .catch(error => {
        console.log('Could not read photo file');
      })
    })
    .catch(_ => {
      console.error('Confirmation failed. file was NOT created');
    })
  })
  .catch(err => {
    console.error(dir + 'profile-photo.jpg NOT written');
  })
}

来自ProfilePage:

ionViewWillEnter() {
    console.log('profile.ts Pre-defined URL: ' + this.user.photoURL);
    if (!this.user.photoURL || this.user.photoURL == this.default_photo_path) {
        this.file.checkFile(this.file.dataDirectory + 'images', 'profile-photo.jpg')
        .then(_ => {
            this.user.photoURL = this.file.dataDirectory + 'images/profile-photo.jpg';
            console.log(this.file.dataDirectory + 'images/profile-photo.jpg exists')
            console.log(this.user.photoURL);
        })
        .catch(_ => {
            this.user.photoURL = this.default_photo_path;
            console.log(this.file.dataDirectory + 'images/profile-photo.jpg does NOT exist');
            console.log(this.user.photoURL);
        })
    }
}

以下是生成的日志

[02:09:35]  console.log: ionViewDidLoad ProfilePage
[02:09:35]  console.log: profile.ts Pre-defined URL: undefined
[02:09:35]  console.log: file:///data/user/0/com.awedsoftware.calledout/files/images/profile-photo.jpg does NOT exist
[02:09:35]  console.log: assets/imgs/default-user.png
[02:09:36]  console.log: ionViewDidLoad CameraPage
[02:09:36]  console.log: Camera started
[02:09:38]  console.log: Picture success
[02:09:38]  console.log: file:///data/user/0/com.awedsoftware.calledout/files/
[02:09:38]  console.log: file:///data/user/0/com.awedsoftware.calledout/files/images/ exists
[02:09:38]  console.log: file:///data/user/0/com.awedsoftware.calledout/files/images/profile-photo.jpg written
[02:09:38]  console.log: Confirmed. File was created
[02:09:38]  console.log: ionViewDidLoad ProfilePage
[02:09:38]  console.log: profile.ts Pre-defined URL: assets/imgs/default-user.png
[02:09:38]  console.log: file:///data/user/0/com.awedsoftware.calledout/files/images/profile-photo.jpg does NOT exist
[02:09:38]  console.log: assets/imgs/default-user.png

注意,在日志中,文件已创建。我确认文件已创建,并读取它的内容。为了保持清洁,我注释了打印内容的代码,但请放心,它做了正确的事情。然后应用程序推送到ProfilePage,在ionViewWillEnter()上,文件再次检查存在,并且它不再存在。我哪里出错了?

谢谢!

0 个答案:

没有答案