0

I have file system code that renames a file in the documents folder within a NativeScript JavaScript application. It's worked for years on both iOS and Android, but now fails consistently on Android 12.

The code looks like this:

const fs = require("@nativescript/core/file-system");    

let documentsPath = fs.knownFolders.documents().path; 
let filePath = fs.path.join(documentsPath, filename);
let tempPath = fs.path.join(documentsPath, filename + ".tmp");

let newFile = await http.getFile(httpRequestOptions, tempPath); // file object
if (fs.File.exists(filePath)) { // if older data file exists 
  let oldFile = fs.File.fromPath(filePath); 
  oldFile.removeSync((error) => {
    console.log("removeError: " + error );
  }); 
}

newFile.renameSync(filename,(error) => {
  console.log("renameError: " + error );
});

On Android 12, this code now fails with the message,

renameError: Error: Failed to rename file '/data/user/0/tech.govia.festivelo/files/FV_22_Thurs_Long.pdf.tmp' to 'FV_22_Thurs_Long.pdf'

But doesn't give any insight into why it failed. This occurs on a Samsung Galaxy S10e device.

I can create and delete files successfully, but can't rename. I'd welcome any insights here, as I haven't found anything in my searches.

Edit Sep 6, 2022: for what it's worth, I can read the new file's content and write it out to a new file to effectively perform a copy, so I have a hacky workaround, 'tho it's way less efficient that simply renaming.

David
  • 578
  • 3
  • 16

1 Answers1

0

Turns out a fully-qualified path is required on Android 12, not just a filename, so changing

newFile.renameSync(filename,(error) => {
  console.log("renameError: " + error );
});

to

newFile.renameSync(filePath,(error) => {
  console.log("renameError: " + error );
});

Is sufficient to fix the problem.

Much thanks to triniwiz on the {N} Discord for the pointer.

Edit Sep 8 2022: Testing on iOS reveals file name is still needed, and using a full path causes an error.

David
  • 578
  • 3
  • 16