問題描述
我已經(jīng)使用官方 API 編寫了一個應(yīng)用程序來上傳到 Google 驅(qū)動器,這非常有效.但是我無論如何都找不到取消上傳.我在 ASyncTask 中運行它,到目前為止,我已經(jīng)嘗試使驅(qū)動器對象、文件路徑、令牌和其他一些變量都沒有.我也嘗試過使令牌無效,但似乎這只是將其從緩存中刪除,實際上并沒有使其服務(wù)器端無效.我在 ASyncTask 上調(diào)用了取消,但是如果在上傳開始后取消它,似乎無法停止它.
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 的文件,除了打開和關(guān)閉互聯(lián)網(wǎng)連接之外,他們無法取消它,這是不切實際的.這是我的上傳代碼:
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
但是,根據(jù)該庫的當前設(shè)計,這可能是一個難以實現(xiàn)的功能.
However, this may a difficult feature to implement based on the current design of that library.
另一個要考慮的選項是不使用 AsyncTask,而是自己實現(xiàn)多線程并中止正在運行的線程.不愉快,但可能是您現(xiàn)在這樣做的唯一選擇.
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.
這篇關(guān)于可以取消 Google 云端硬盤上傳嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!