Android - Google Drive Api:如何知道上传任务何时完成?

时间:2017-03-17 16:03:58

标签: java android google-drive-api google-drive-android-api

我可以将许多文件上传到云端硬盘,但我不知道任务何时完成。 结果回调让我很好,但如果我关闭互联网连接,那么任务就会中断。这是我使用的代码:

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

我想知道何时确定所有文件都已上传到云端(因此我可以在本地删除它们)。

1 个答案:

答案 0 :(得分:1)

您使用的是Google云端硬盘Android API。您使用GDAA创建的所有驱动器文件都是在设备上创建的。创建后,您可以删除原始文件。一段时间后,GDAA会将Drive File与云同步。最后一个方面对您来说是不可见的,可以忽略。