i have the following code
which is supposed to upload an image from my android device to my google Drive.
private void saveFileToDrive() {
progressDialog = ProgressDialog.show(this, "", "Loading...");
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
// File's binary content
java.io.File fileContent = new java.io.File(fileUri.getPath());
FileContent mediaContent = new FileContent("image/jpeg", fileContent);
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("image/jpeg");
File file = service.files().insert(body, mediaContent).execute();
if (file != null) {
showToast("Photo uploaded: " + file.getTitle());
progressDialog.dismiss();
//startCameraIntent();
}
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
}
The thing is the upload never ends. That's weird as the image not too big
and I guess if I would have upload it via original Drive App, it would be done faster.
1) how can I show the upload progress?
2) how can I make a callback within the same Activity after the upload is done?
Downloading file from Google Drive:
You can either use an AsyncTask in which you can obtain an inputstream from the url and write a while loop for reading each line and showing the downloaded data on a dialog as below.
You can use the inherent Google Drive's Download Listeners which can be set with insert object.
Uploading File to Google Drive :
We can upload files to google drive using Google own implementation of MediaHttpUploaderProgressListener as below :