我創建了一個AsyncTask來上傳或下載我的Google雲端硬盤中的文件。Android如何停止/終止/取消媒體上傳或下載
這是我的代碼部分的示例上傳文件:
public class AsyncUpload extends AsyncTask<String, String, String>{
Context c;
public AsyncUpload(Context c) {
super();
this.c = c;
}
@Override
protected String doInBackground(String... params) {
try{
// File's binary content
java.io.File fileContent = new java.io.File(localFilePath);
InputStreamContent mediaContent = new InputStreamContent(FileInfo.getMime(localFilePath), new BufferedInputStream(new FileInputStream(fileContent)));
mediaContent.setLength(fileContent.length());
// File's metadata.
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType(FileInfo.getMime(localFilePath));
if(parentId!=null){
ParentReference parent = new ParentReference();
parent.setId(parentId);
body.setParents(Arrays.asList(parent));
}
Drive.Files.Insert request = drive.files().insert(body, mediaContent);
MediaHttpUploader uploader = request.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE);
uploader.setProgressListener(mediaHttpUploaderProgressListener);
File file = request.execute();
if (file != null) {
Log.i(TAG,"File Uploaded");
isAttempt=false;
return file;
}
} catch (IOException e) {
Log.e(TAG,"IOEXCEPTION Message:\n"+e.getMessage());
}
return null;
}
}
此代碼的工作完美,但我不知道如何阻止它,而一個文件被上傳(或與下載類似的代碼)!
請幫幫我!
非常感謝
感謝@RohanKandwal,但那裏有什麼代碼來阻止下載/上傳,而他們正在工作? – Meroelyth