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:
- Resumable upload is much slower than direct upload because it pauses frequently
- With 1MB chunks, uploads fail around 70% completion for 10MB files
- 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?