I can’t seem to find any info on the error I’m getting when trying to copy a file taken with the camera to data storage. Here is my function (which is pretty much what everyone seems to do):
copyFileToLocalDir() {
let d = new Date();
let n = d.getTime();
let newFileName = n + ".jpg";
console.log('new name',newFileName);
this.file.copyFile(this.imagePath, this.imageName, cordova.file.dataDirectory, newFileName)
.then(success => {
this.lastImage = newFileName;
}, error => {
console.log('error saving');
});
}
The error I’m getting when running in Android emulator is ERROR Error: Uncaught (in promise): Invalid action
Ionic Info:
So I had to dig further and better understand what File plugin is doing. And thanks to the answers on these two questions here and here, I finally figured this thing out. Here’s my final code:
copyFileToLocalDir() {
let d = new Date();
let n = d.getTime();
let newFileName = n + ".jpg";
// cordova.file.dataDirectory
let externalStoragePath: string = cordova.file.dataDirectory;
this.file.resolveLocalFilesystemUrl(this.imagePath + this.imageName)
.then((entry: any)=>{
console.log('entry',entry);
this.file.resolveLocalFilesystemUrl(externalStoragePath)
.then((dirEntry: any)=>{
entry.copyTo(dirEntry, newFileName, this.successCopy, this.failCopy);
}).catch((error)=>{
console.log(error);
});
}).catch((error)=>{
console.log(error);
});
}
You have to create 2 objects, one of the current file (entry) and one for the path to copy the file to (dirEntry). That was the key to working this out.