I have written an app using the official API to upload to Google drive, this works perfectly. However I can't find anyway to cancel an upload. I'm running it in an ASyncTask, so far I have tried making the drive object, the file path, the token and a few other variables none. I've also tried to invalidate the token however it seems this only removes it from the cache and doesn't actually invalidate it server side. I've called cancel on the ASyncTask however if it is cancelled once the upload has started there seems to be no way to stop it.
My issue is if a user starts uploading a 100mb file there's no way for them to cancel it other then turning their internet connection on and off which isn't practical. This is my upload code:
java.io.File jFile = new java.io.File(path); //Does not get file just info
File body = setBody(jFile, mimeType);
try
{
java.io.File mediaFile = new java.io.File(path);
InputStreamContent mediaContent =
new InputStreamContent(mimeType,
new BufferedInputStream(new FileInputStream(mediaFile)));
mediaContent.setLength(mediaFile.length());
request = drive.files().insert(body, mediaContent);
request.getMediaHttpUploader().setProgressListener(new CustomProgressListener());
File file = request.execute();
Surely there is a way to cancel an upload?
If you have an Android, it's typically in the app drawer. Tap the grayed-out file that says Upload paused. This automatically resumes the upload from where it was paused.
The application will resume uploads even if you shut down during a sync operation. Files sent there will upload as Internet is connected. If however you do so from a browser this may not be the case. Some systems may view this the upload as activity and thus not sleep until the upload is complete.
If your storage plan is canceled or expires Everything in Google Drive, Google Photos, and Gmail will still be accessible, but you won't be able to create or add anything new over the free storage limit.
Great question! I filed a feature request for the ability to cancel a media upload request:
https://code.google.com/p/google-api-java-client/issues/detail?id=671
However, this may a difficult feature to implement based on the current design of that library.
Another option to consider is to not use AsyncTask and instead implementing the multi-threading yourself and abort the running Thread. Not pleasant, but may be your only option for doing this now.
In a comment I have said that putting the request in a thread and call interrupt is not working. So I was searching another way of doing it.
I have finally found one, an ugly one: Close the InputStream of the uploaded file!
I initialize with:
mediaContent = new InputStreamContent(NOTE_MIME_TYPE,new BufferedInputStream(new FileInputStream(fileContent)));
mediaContent.setLength(fileContent.length());
And in a stop listener I have :
IOUtils.closeQuietly(mediaContent.getInputStream());
For sure it should be possible to do it in a better way!
Regards
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With