Preserving original file modification timestamp when uploading to Google Drive via Android API

Hey everyone,

I’m working on an Android app that uploads files to Google Drive using their API. I’ve run into a problem where the uploaded files are getting new modification timestamps instead of keeping their original ones.

Here’s what I’ve tried:

File fileMetadata = new File();
fileMetadata.setName("MyFile.txt");
fileMetadata.setModifiedTime(DateTime.parseRfc3339("2023-05-15T10:30:00Z"));

FileContent mediaContent = new FileContent("text/plain", localFile);

File file = driveService.files().create(fileMetadata, mediaContent)
    .setFields("id, modifiedTime")
    .execute();

But it’s not working as expected. The files still show the upload time as the modified time.

Has anyone else faced this issue? How did you solve it? I’d really appreciate any tips or workarounds. Thanks in advance!

hey jackhero, ran into similar issue. try using setmodifiedtime() after the upload instead of before. like this:

File file = driveService.files().create(fileMetadata, mediaContent).execute();
file.setModifiedTime(new DateTime(originalModifiedTime));
driveService.files().update(file.getId(), file).execute();

hope that helps!

I’ve dealt with this issue before, and it can be tricky. One thing that worked for me was using the ‘setProperties’ method to store the original modification time as a custom property. Then, you can retrieve and display this property instead of relying on the Drive-assigned timestamp. Here’s a rough example:

File fileMetadata = new File();
fileMetadata.setName(“MyFile.txt”);
fileMetadata.setProperties(Collections.singletonMap(“originalModTime”, originalTimestamp));

FileContent mediaContent = new FileContent(“text/plain”, localFile);

File file = driveService.files().create(fileMetadata, mediaContent)
.setFields(“id, properties”)
.execute();

This approach doesn’t change the actual modification time in Drive, but it allows you to store and access the original timestamp when needed. You might need to adjust your app’s UI to display this custom property instead of the standard ‘modifiedTime’ field.

I encountered this issue in my project as well. The solution that worked for me was to use the ‘createdTime’ field instead of ‘modifiedTime’. Google Drive API seems to handle ‘createdTime’ more reliably for preserving original timestamps. Here’s the adjusted code:

File fileMetadata = new File();
fileMetadata.setName("MyFile.txt");
fileMetadata.setCreatedTime(new DateTime(originalFileTimestamp));

FileContent mediaContent = new FileContent("text/plain", localFile);

File file = driveService.files().create(fileMetadata, mediaContent)
    .setFields("id, createdTime")
    .execute();

This approach maintained the original file timestamps in my case. If it doesn’t work, you might need to check your app’s permissions or consider using a different API method for uploads.