문제

I create a AsyncTask to Upload or Download files on my Google Drive.

This is an example of a part of my code to upload a file:

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

}

This code works perfectly but I don't know how to stop it while a file is uploading (or downloading with a similar code)!

Please help me!

Many Thanks

도움이 되었습니까?

해결책

try this: the first call cancel method on the instance of AsyncTask

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

        @Override
        public void onClick(View arg0) {
            mTask.cancel(true);
        }
    });

Then you need to add check if your async task has been canceled:

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

Hope this will work.

Have to add following - this is not a 'right' way how to implement transferring operations. Keep in mind that your activity may be stopped and even destroyed and than created again while you task is working. Event worse thing may happen. Running AsyncTask cannot prevent destroying the whole app when system runs out of memory while transferring is still going on if your app does not have any activity in foreground. In this case transferring may even not being completed.

It might be better to consider moving transferring operations to a dedicated service component.

다른 팁

If you cancel the AsyncTask then your Download and Upload automatically cancel

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

Thanks

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top