Cancel Google Drive upload possible?(可以取消 Google Drive 上传吗?)
问题描述
我已经使用官方 API 编写了一个应用程序来上传到 Google 驱动器,这非常有效.但是无论如何我都找不到取消上传的方法.我在 ASyncTask 中运行它,到目前为止,我已经尝试将驱动器对象、文件路径、令牌和其他一些变量设置为无.我还尝试使令牌无效,但是似乎这只会将其从缓存中删除,而实际上并未使其服务器端无效.我已经在 ASyncTask 上调用了取消,但是如果它在上传开始后被取消,似乎无法阻止它.
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.
我的问题是,如果用户开始上传 100 mb 的文件,他们无法取消它,只能打开和关闭他们的互联网连接,这是不切实际的.这是我的上传代码:
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?
推荐答案
好问题!我提交了取消媒体上传请求的功能请求:
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.
另一个需要考虑的选择是不使用 AsyncTask 而是自己实现多线程并中止正在运行的线程.不愉快,但可能是您现在这样做的唯一选择.
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.
这篇关于可以取消 Google Drive 上传吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:可以取消 Google Drive 上传吗?


基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01