Search code examples
androidkotlinandroid-file

In Android, can a file be moved to inside the app's private directory?


I have a file in the downloads directory. Let's call it OldFile, of the File class.

I seem to be move it anywhere I like using OldFile.renameTo(File(some new file path)) UNLESS the new file path is inside the application's private file directory as obtained from context.filesDir.

This Kotlin snippet works, for instance:

val newFileLocation = File(Environment.getExternalStorageDirectory().absolutePath + "/test.txt")
oldFile.renameTo(newFileLocation)

but this snippet fails silently, throwing no exception, leaving oldFile completely unchanged:

val newFileLocation = File(context?.filesDir?.absolutePath + "/test.txt")
oldFile.renameTo(newFileLocation)

Is there some rule against using File.renameTo to move a file into the filesDir path?

How am I supposed to go about it?

thanks

John


Solution

  • The renameTo() works only for files on the same 'partition'.

    Apparently getFilesDir() is on a different partition.