Preserve original file timestamps when uploading to Google Drive API Android

I’m working on an Android app that uploads files to Google Drive using their API. I’ve run into an issue where every uploaded file gets a new modified timestamp showing when it was uploaded, instead of keeping the original file’s modification date.

When I upload files through the regular Google Drive desktop application, it correctly preserves the original timestamps. But with the API, this doesn’t happen automatically.

I’ve attempted to fix this by using the setModifiedDate() function on the File object before uploading, but it’s not working as expected. I’m wondering if maybe I’m using the wrong date format. I’ve tested these two timestamp formats:

2012-08-09T05:34:36-07:00
2012-08-09T05:34:36-0700

Both formats failed to preserve the original date. Is there a built-in way to automatically maintain the file’s original modification time during API uploads? Or if manual setting is required, what’s the correct approach to make setModifiedDate() work properly?

I’m following the standard file insertion pattern from the official documentation, but this timestamp issue is causing problems for my users who need to maintain file chronology.

Had the same issue building a document archival app. Your date format’s fine - it’s actually about API permissions and metadata handling. You need to explicitly request permission to modify timestamps by adding “modifiedTime” to your update request. When creating the File metadata object, use setModifiedTime() with ISO 8601 format, but here’s the key part: call setKeepRevisionForever(true) on your File object before upload. Skip this and Drive treats it as a new revision and auto-updates the timestamp. Also make sure your app has drive.file scope permission, not just drive.readonly. The desktop client works because it uses internal APIs with broader permissions. After setting metadata, double-check by querying the file’s modifiedTime field right away to confirm it matches your original timestamp.

The timestamp format isn’t your only problem - you’re not handling the request parameters right. I ran into this exact issue on a file sync project. Setting metadata by itself won’t work. You need setKeepRevisionForever(false) and make sure to include addParents() with the right folder ID if you’re putting it somewhere specific. Here’s the key: use Files.Update, not Files.Create. Create the file first with basic metadata, then immediately hit it with a second API call to update the timestamp. This two-step process stops Drive from overwriting your custom modification time. Double-check that your OAuth scope has https://www.googleapis.com/auth/drive.file with write permissions. Some restricted scopes will block timestamp changes even when the upload works fine. Try a hardcoded timestamp first to figure out if it’s your date parsing or the API setup that’s broken.

check your api version first. i had the same issue - was mixing v2 and v3 calls without knowing it. also make sure you’re reading the original file’s lastModified timestamp right from the android filesystem before setting it on Drive. sometimes it’s not the Drive API that’s broken, it’s how you’re pulling the original date from local files.

The setModifiedDate() approach works, but you’re missing a step. After setting the modified date on your File object, you need to include “modifiedTime” in your setFields parameter when calling the Drive API. I hit this same issue last year building a backup app. Use RFC 3339 format with proper timezone handling: “2012-08-09T05:34:36.000Z” (the .000Z ending is for UTC). Also, use setModifiedTime() instead of setModifiedDate() if you’re on newer API versions. Here’s another gotcha - some Android file systems don’t preserve milliseconds in timestamps, so you might need to truncate to seconds only. Google Drive’s desktop client handles this automatically, but the API doesn’t. Double-check that you’re reading the local file timestamp correctly before upload. I’ve seen the real problem be in extracting local file metadata, not setting it on Drive.

Everyone’s suggesting the API approach, but you’ll hit tons of edge cases. Android versions handle file metadata differently, manufacturers mess with timestamps, and you’ll waste weeks debugging timezone issues and permissions.

Skip the mobile API headache. Set up automation where your Android app just sends files, then let that handle the Drive uploads with proper timestamps.

The automation reads original metadata, formats timestamps for Drive API, manages permissions, and retries failed uploads. Your app just triggers it.

I’ve watched this exact problem kill release schedules when devs try handling every Android filesystem quirk manually. Automation works across all devices and OS versions without constant code updates.

Bonus: you can add batch processing or backup verification without touching mobile code.

Latenode handles these file operations really well: https://latenode.com

The Problem:

You’re uploading files to Google Drive using the API, and the uploaded files are receiving a new modified timestamp instead of retaining their original modification time. This is inconsistent with the behavior of the Google Drive desktop application, which correctly preserves the original timestamps. You’ve tried using the setModifiedDate() function, but it hasn’t resolved the issue.

TL;DR: The Quick Fix:

Don’t fight with manual timestamp setting. Automate the entire upload process using a workflow tool that handles metadata correctly. This avoids the complexities of the Google Drive API and ensures consistent timestamp preservation across different Android versions and devices.

:thinking: Understanding the “Why” (The Root Cause):

The Google Drive API’s behavior regarding file metadata, specifically modification timestamps, differs from the desktop application’s internal handling. Manually setting the timestamp using setModifiedDate() often fails due to subtle nuances in API interactions, Android OS variations, and permission issues. The root cause isn’t necessarily a coding error in your app, but rather the intricacies and inconsistencies of the API and its interaction with diverse Android environments. Directly addressing this requires a deep understanding of the API’s handling of metadata updates and potential conflicts. Attempting to manage these complexities through manual code adjustments is error-prone and likely to lead to further compatibility issues. A more robust solution bypasses these challenges entirely.

:gear: Step-by-Step Guide:

  1. Utilize a Workflow Automation Tool: Instead of directly interacting with the Google Drive API from your Android application, use a workflow automation tool (like Latenode, mentioned in several forum responses) to handle the file uploads. This tool should allow you to define a workflow that performs the following actions:

    • File Retrieval: Fetch the file from your Android application’s storage.
    • Metadata Extraction: Extract the original file’s modification timestamp from its metadata. The tool should automatically handle the complexities of different Android file systems and their metadata representations.
    • Google Drive Upload: Upload the file to Google Drive using the appropriate API calls.
    • Metadata Preservation: Crucially, the workflow should set the original modification timestamp as metadata during the upload process using the correct API parameters and handling. It should handle any necessary format conversions (e.g., converting the timestamp to RFC 3339 format) and timezone adjustments automatically.
    • Error Handling and Retries: Implement robust error handling and retry mechanisms to ensure reliability across various network conditions.
  2. Integrate with Your Android App: Integrate the workflow automation tool into your Android application. This integration should involve a simple API call or message passing mechanism to trigger the upload workflow when a user selects a file for upload. Your Android app only needs to send the file to the workflow; the workflow handles the rest, including the timestamp preservation.

:mag: Common Pitfalls & What to Check Next:

  • Android File System Variations: Different Android versions and device manufacturers might handle file timestamps differently. Manual timestamp management in your code can lead to compatibility problems.
  • API Permissions: Ensure your application has the necessary Google Drive API permissions (drive.file scope with write permissions) to modify file metadata.
  • Timezone Handling: Improper timezone handling can cause timestamp inconsistencies. The workflow automation tool should automatically handle timezone conversions.
  • Rate Limits: Be aware of Google Drive API rate limits and implement error handling to manage situations where requests are throttled.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.