Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android如何停止/杀死/取消媒体上传或下载_Android_File Upload_Android Asynctask_Download_Media - Fatal编程技术网

Android如何停止/杀死/取消媒体上传或下载

Android如何停止/杀死/取消媒体上传或下载,android,file-upload,android-asynctask,download,media,Android,File Upload,Android Asynctask,Download,Media,我创建了一个AsyncTask来上传或下载谷歌硬盘上的文件 这是我上传文件的部分代码示例: public class AsyncUpload extends AsyncTask<String, String, String>{ Context c; public AsyncUpload(Context c) { super(); this.c = c; } @Override protected St

我创建了一个AsyncTask来上传或下载谷歌硬盘上的文件

这是我上传文件的部分代码示例:

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;
    }

}
公共类AsyncUpload扩展了AsyncTask{
上下文c;
公共异步上载(上下文c){
超级();
这个.c=c;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
//文件的二进制内容
java.io.File fileContent=新的java.io.File(localFilePath);
InputStreamContent mediaContent=新的InputStreamContent(FileInfo.getMime(localFilePath),新的BufferedInputStream(新的FileInputStream(fileContent));
setLength(fileContent.length());
//文件的元数据。
文件体=新文件();
setTitle(fileContent.getName());
setMimeType(FileInfo.getMime(localFilePath));
if(parentId!=null){
ParentReference parent=新的ParentReference();
parent.setId(parentId);
setParents(Arrays.asList(parent));
}
Drive.Files.Insert请求=Drive.Files().Insert(正文,mediaContent);
MediaHttpUploader uploader=request.getMediaHttpUploader();
uploader.setDirectUploadeEnabled(false);
uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE);
setProgressListener(mediaHttpUploaderProgressListener);
File=request.execute();
如果(文件!=null){
Log.i(标记“上传的文件”);
isAttempt=假;
返回文件;
}
}捕获(IOE异常){
Log.e(标记“IOEXCEPTION Message:\n”+e.getMessage());
}
返回null;
}
}
这段代码工作得很好,但我不知道如何在文件上传(或使用类似代码下载)时停止它

请帮帮我


非常感谢

如果您取消了异步任务,那么您的下载和上载将自动取消

if(mTask!=null) mTask.cancel();
谢谢

试试这个: AsyncTask实例上的第一个调用cancel方法

    findViewById(R.id.cancelButton).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            mTask.cancel(true);
        }
    });
然后,您需要添加检查您的异步任务是否已取消:

    @Override
    protected String doInBackground(String... params) {

        try{
            // File's binary content
            java.io.File fileContent = new java.io.File(localFilePath);
            final FileInputStream fileStream = new FileInputStream(fileContent);
            InputStreamContent mediaContent = new InputStreamContent(FileInfo.getMime(localFilePath), new BufferedInputStream(fileStream));

            ....

            uploader.setProgressListener(new MediaHttpUploaderProgressListener() {
                void progressChanged(MediaHttpUploader uploader) {
                    if(isCancelled()) {
                        // could not find another way to cancel transferring except that:
                        fileStream.close();
                    }
                    else {
                        mediaHttpUploaderProgressListener(uploader);
                    }
                }
            });

            ....

        } catch (IOException e) {
            Log.e(TAG,"IOEXCEPTION Message:\n"+e.getMessage());
        }

        return null;
    }
希望这能奏效

必须添加以下内容-这不是实现传输操作的“正确”方法。 请记住,当您的任务正在工作时,您的活动可能会停止,甚至被销毁,然后再次创建。 更糟糕的事情可能会发生。如果应用程序在前台没有任何活动,则在传输仍在进行时,系统内存不足时,运行AsyncTask无法阻止销毁整个应用程序。在这种情况下,传输甚至可能无法完成


最好是考虑把移动操作转移到一个专用的服务组件上。

谢谢@ RohanKandwal,但是在他们工作的时候,有一个代码来停止下载/上传吗?你必须检查被取消的内部doI背景,停止处理自己的感谢,但是我必须使用代码吗?请给我一个关于如何停止进程的例子(下载/上传)…从我的活动的OnCreate()开始,我用以下方式启动代码:AsyncUpload asyncUpl=new AsyncUpload(this);asyncUpl.execute(“”);我在同一活动中有一个按钮:bt_store.setOnClickListener(new OnClickListener(){@Override public void onClick(视图arg0){asyncUpl.cancel(true);}});从活动的OnCreate()开始,我以如下方式启动此代码:AsyncUpload asyncUpl=new AsyncUpload(this);asyncUpl.execute(“”);我在同一活动中有一个按钮:bt_store.setOnClickListener(new OnClickListener(){@Override public void onClick(视图arg0){asyncUpl.cancel(true);}});但是asynctask的“doInBackground”方法继续运行!非常感谢大家!!!它工作得很好!现在我在服务中使用此代码!!!你是最棒的!