可以取消 Google 云端硬盘上传吗?

时间:2023-03-18
本文介绍了可以取消 Google 云端硬盘上传吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用官方 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.

我的问题是,如果用户开始上传一个 100mb 的文件,除了打开和关闭互联网连接之外,他们无法取消它,这是不切实际的.这是我的上传代码:

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 云端硬盘上传吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何在 Eclipse 中将源附加到 Google Drive API 或“A 下一篇:使用 Java 将数据写入 Google 电子表格

相关文章

最新文章