Search code examples
androidiosfileappcelerator

How to properly rename and move files with Appcelerator/Titanium?


When the user takes a camera, the file is stored in the tempDirectory folder. The file is renamed and the moved to an appImages folder (in the applicationDataDirectory). This http://docs.appcelerator.com/platform/latest/#!/guide/Filesystem_Access_and_Storage was helpful.

The code below fully works with Android, but with iOS the files are only renamed, but not moved. Any ideas?

var fs = Ti.Filesystem; 
var imageDir = fs.getFile(fs.applicationDataDirectory,'appImages');
    if (! imageDir.exists()) {
        imageDir.createDirectory();
    }
var tempFile = fs.getFile(fs.tempDirectory,timeStamp + "_" + registeredUserID + "_nomination.jpg"); 
    tempFile.rename(nid + "_" + registeredUserID + "_nomination.jpg"); //renames nomination image
    tempFile = fs.getFile(fs.tempDirectory,nid + "_" + registeredUserID + "_nomination.jpg"); //gets the new handler
    tempFile.move("appImages/" + nid + "_" + registeredUserID + "_nomination.jpg"); //moves nomination image

var tempListAfter = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory).getDirectoryListing();
    Ti.API.info("tempListAfter AFTER moving: " + JSON.stringify(tempListAfter));
    var appImagesListAfter = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'appImages').getDirectoryListing();
    Ti.API.info("appImagesListAfter AFTER moving: " + JSON.stringify(appImagesListAfter)); //iOS results an empty array, but works in Android and shows the properly named files in Android
    tempFile = tempThumbFile = null;

solution based on Mituls suggestion:

tempFile.move(imageDir.nativePath + nid + "_" + registeredUserID + "_nomination.jpg");

Solution

  • I think you should use imageDir.nativePath to move tempFile like

    tempFile.move(imageDir.nativePath);