Monitor file transfer progress with Google Drive API in Java Android

I’m working on an Android app that uploads and downloads files using Google Drive API. I need help tracking the transfer progress in real-time.

Upload Progress Issue

Currently I’m using driveService.files().create(fileMetadata, content).execute() but this method only returns the final result after completion. I want to show progress updates every few seconds while the upload is happening.

Download Progress Question

For downloads, I’m thinking about reading the InputStream byte by byte and comparing it to the total file size to calculate percentage. Is this the right approach or are there better methods?

Sample Code

private void uploadDocument() {
    new Thread(() -> {
        try {
            java.io.File localFile = new java.io.File(documentPath);
            FileContent content = new FileContent("application/pdf", localFile);
            
            File metadata = new File();
            metadata.setName(localFile.getName());
            metadata.setMimeType("application/pdf");
            
            File result = driveService.files().create(metadata, content).execute();
            if (result != null) {
                displayMessage("Document uploaded: " + result.getName());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }).start();
}

Update: Tried Resumable Upload

I attempted using resumable uploads with chunk size set to 1MB:

Drive.Files.Create createRequest = driveService.files().create(metadata, content);
MediaHttpUploader uploader = createRequest.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
uploader.setChunkSize(1024 * 1024);
uploader.setProgressListener(new UploadProgressListener());
File uploadedFile = createRequest.execute();

Problems I’m facing:

  1. Resumable upload is much slower than direct upload because it pauses frequently
  2. With 1MB chunks, uploads fail around 70% completion for 10MB files
  3. Larger chunks work but don’t show meaningful progress

What’s the best way to get real-time progress for both uploads and downloads without sacrificing performance?

check your internet connection first - 70% failure rates usually mean your connection’s dropping mid-transfer. try using an okhttp interceptor to monitor the actual network traffic. that’s how i debugged similar Drive API issues. and make sure you’re handling resume tokens correctly when chunks fail. most people just restart from the beginning, which is a waste.

Had the same issues with Drive API progress tracking on Android last year. You’re right about 1MB chunks being too small - they create way too much overhead for larger files. Bump your upload chunk size to 8MB or 16MB. Cuts down requests while still giving decent progress updates. I’ve found 8MB hits the sweet spot for files under 100MB. For downloads, skip the byte-by-byte approach. Use a 64KB or 128KB buffer instead, then calculate progress from total bytes read vs content length in the response headers. Way more efficient. Couple things that saved me headaches: Add a timeout for progress updates - if nothing happens for 30+ seconds, restart the resumable session. And handle network switches properly since Android loves jumping between WiFi and mobile data mid-transfer, which kills resumable uploads. Yeah, resumable uploads will always be slower than regular uploads, but proper chunk sizing makes a huge difference.

That 70% chunk failure screams memory issue, not network. Had the same crashes with Drive API uploads last summer. Set your chunk size to exactly 262144 bytes (256KB) - skip the standard power-of-2 values. Google’s servers handle this size way better for whatever reason. For progress listeners, make sure you’re hitting the UI thread with runOnUiThread() or Handler.post(). My progress callbacks were all over the place until I fixed the threading. Also check if you’re handling HTTP 308 resume responses properly. Drive API throws these during chunked uploads and if you’re not catching them right, you’ll get those 70% failures. For downloads, use buffered reading with 8KB chunks and track against the Content-Length header. Way smoother than byte-by-byte and won’t tank your performance.