0

FileUtils.copyDirectory doesn't write date modified correctly unless the Android SD card is unmounted. Using this simple bit of code from FileUtils:

    try {
        FileUtils.copyDirectory(srcDir2, destDir2);
    } catch (IOException e) {

I can copy a directory from the internal storage on the phone to the sd card preserving the date modified information on the files in the directory which is essential for my app.

Sadly if the SD card is removed without ejecting it all the date modified information on the files in the copied dir is set to the time the files were copied. if the SD is unmounted correctly then the date modified information is preserved correctly.

I have tried the flush and close functions but they are not relevant to this kind of file. What code am I missing to finalize the directories without unmounting? I am using an android device will a full size SD slot and I cant risk loosing all the information if it gets knocked out without a proper eject

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79

1 Answers1

0

There's a version of this method that accepts a flag as third parameter to try to control the date of last modification. You can check the docs here. But looks like the method you are using also tries to preserve the date by default. In fact, reading the source code, the method you are using just calls this method with the third parameter set to true.

In the docs for your method it is said:

Note: This method tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.

In the end what this library does each time it copies a file or dir is to call File.setLastModified over the destination file with the last modified date of the source file. This method has been reported to be unreliable in Android, as you can see in these other questions:

Android set last modified time for the file
file.lastModified() is never what was set with file.setLastModified()
Is it possible to reset the last modified date of an Android file?

But in your case, I think you are trying to provide a workaround for something that is just designed that way. I'm no expert, but this is managed by either the OS or the FileSystem. The unmounting mechanism serves a purpose, I think you can't do much about it as an app developer.

Community
  • 1
  • 1
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
  • >edit: having looked at the links above im not sure this is the same issue. for me Fileutils and File.setLastModified are both working i just need a way to force the android OS to write any remaining info that it is hanging onto without having to unmount the card – user3526535 May 21 '14 at 10:43
  • Even if the problem had nothing to do with `setLastModified`, I think you are fighting against the OS/filesystem here. Android supports a lot of FS. Changes to files might not be flushed to the SDCard immediately. Removing the SDCard like that is simply and plain a bad practice. – Mister Smith May 21 '14 at 11:26
  • totally agree but sometimes these things cant be helped. user error or sd gets knocked out etc. in this situation i dont really want to loose the correct date modified info. thanks for your help though : ) is a real pain – user3526535 May 21 '14 at 14:45