我创建了一个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