1

I'm testing Google Drive API with this code :

public void UploadVideo() {
    Drive.DriveApi.newDriveContents(gac).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
        public void onResult(final DriveApi.DriveContentsResult result) {
            new Thread() {
                public void run() {
                    BufferedInputStream bis = null;
                    BufferedOutputStream bos = null;
                    try {
                        bis = new BufferedInputStream(new FileInputStream("/sdcard/my_video.avi"));
                        bos = new BufferedOutputStream(result.getDriveContents().getOutputStream());

                        final int BUFFER_SIZE = 8192;
                        byte buffer[] = new byte[BUFFER_SIZE];
                        int n;

                        while ((n = bis.read(buffer, 0, BUFFER_SIZE)) != -1)
                            bos.write(buffer, 0, n);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    finally {
                        if(bis != null) {
                            try {
                                bis.close();
                            } catch (IOException e) {
                            }
                        }
                        if(bos != null) {
                            try {
                                bos.close();
                            } catch (IOException e) {
                            }
                        }
                    }

                    MetadataChangeSet mcs = new MetadataChangeSet.Builder()
                            .setMimeType("video/x-msvideo")
                            .build();

                    IntentSender is = Drive.DriveApi.newCreateFileActivityBuilder()
                            .setActivityTitle("File name to save")
                            .setInitialMetadata(mcs)
                            .setInitialDriveContents(result.getDriveContents())
                            .build(gac);

                    try {
                        startIntentSenderForResult(is, 54321, null, 0,0,0);
                    } catch (IntentSender.SendIntentException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    });
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (54321 == requestCode) {
        if(resultCode == RESULT_OK) {
            DriveId writtenFileID = data.getParcelableExtra(OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
            DriveFile writtenFile = writtenFileID.asDriveFile();
            writtenFile.getMetadata(gac)
                    .setResultCallback(new ResultCallback<DriveResource.MetadataResult>() {
                        public void onResult(DriveResource.MetadataResult metadataResult) {
                            Metadata md = metadataResult.getMetadata();
                            String s = md.getTitle() + "\n"
                                    + md.getMimeType() + "\n"
                                    + md.getFileSize();
                            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
                        }
                    });
        }
    }
}

The problem is, this works asynchronously EVEN WHEN I CLOSE THE APP, so there's no way to stop the upload. I'm testing with a file sized around 250MB, but all my code above runs IMMEDIATELY TO THE END, including the onActivityResult() callback method. My internet is very slow and I can see the complete uploaded file from my GDrive about in five minutes or so.

Is there any way to stop uploading or synchronous method for uploading?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Jenix
  • 2,996
  • 2
  • 29
  • 58
  • possible duplicate of https://stackoverflow.com/questions/13634894/cancel-google-drive-upload-possible – ReyAnthonyRenacia Jan 14 '18 at 03:36
  • @noogui Could you be more specific with my code, please? Because I just started using GDrive API on Android, I may be wrong, but I read the thread already before posting the question here, and it was very old one(2012) with old API and also seemed outdated. If you can help me with my code, I'll accept your answer. Stopping my worker thread does not help because GDrive alone uploads in the background asyncronously. – Jenix Jan 14 '18 at 05:28
  • FYI, even when I disconnect the Internet on my phone, GDrive API pauses uploading but when the connection is back it resumes. Of course I can't see this process but I can find the resulting file in my GDrive later after testing this way. – Jenix Jan 14 '18 at 05:46

0 Answers0