romanofranz
romanofranz

Reputation: 23

Android - Google Drive Api: how to know when upload task is completed?

I'm able to upload many files to Drive, but I don't know when the task is completed. The result callback gives me ok, but if I close internet connection then the task is interrupted. This is the code I'm using:

    void createFile(DriveFolder pFldr, final String titl, final String mime, final File file) {
    DriveId dId = null;
    setProgressing(true);
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected() && titl != null && mime != null && file != null) try {

        final DriveFolder parent =  pFldr != null ? pFldr : Drive.DriveApi.getRootFolder(mGoogleApiClient);

        Drive.DriveApi.newDriveContents(getGoogleApiClient()).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override public void onResult(DriveApi.DriveContentsResult driveContentsResult) {
                DriveContents cont = driveContentsResult != null && driveContentsResult.getStatus().isSuccess() ?
                        driveContentsResult.getDriveContents() : null;
                if (cont != null) try {
                    OutputStream oos = cont.getOutputStream();
                    if (oos != null) try {
                        InputStream is = new FileInputStream(file);
                        byte[] buf = new byte[4096];
                        int c;
                        while ((c = is.read(buf, 0, buf.length)) > 0) {
                            oos.write(buf, 0, c);
                            oos.flush();
                        }
                    }
                    finally { oos.close();}

                    MetadataChangeSet meta = new MetadataChangeSet.Builder().setTitle(titl).setMimeType(mime).build();

                    parent.createFile(mGoogleApiClient, meta, cont).setResultCallback(new ResultCallback<DriveFolder.DriveFileResult>() {
                        @Override public void onResult(DriveFolder.DriveFileResult driveFileResult) {
                            DriveFile dFil = driveFileResult != null && driveFileResult.getStatus().isSuccess() ?
                                    driveFileResult.getDriveFile() : null;
                            if (dFil != null) {
                                Log.d(TAG,"photo "+count+" uploaded" );
                                count --;
                                if(count == 0){
                                    // is the task completed?
                                }

                            } else {
                                Log.d(TAG,"error during upload photo " + count );
                            }
                        }
                    });
                } catch (Exception e)  {
                    e.printStackTrace();
                    ShowErrorHelper.showErrorDialog(UploadActivity.this, e);
                }
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
        ShowErrorHelper.showErrorDialog(UploadActivity.this, e);
    }
}

I want to know when it's sure that all files are already uploaded to cloud (so I can locally remove them).

Upvotes: 2

Views: 355

Answers (1)

pinoyyid
pinoyyid

Reputation: 22306

You are using the Google Drive Android API. Any Drive Files you create with GDAA are created on the device. As soon as they are created, you can remove your original File. Some time later, GDAA will sync the Drive File with the cloud. This last aspect is invisible to you and can be ignored.

Upvotes: 1

Related Questions