将多个文件上传到Google云端硬盘?

时间:2017-01-14 06:18:36

标签: java android

此处我尝试在google drive app文件夹中上传多个文件:

@Override
protected Boolean doInBackground(DriveFile...params) {

    Drive.DriveApi.requestSync(mGoogleApiClient).await();

    DriveFile file = params[0];
    try {
        DriveApi.DriveContentsResult driveContentsResult = file.open(
            mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, null).await();
        if (!driveContentsResult.getStatus().isSuccess()) {
            return false;
        }
        DriveContents driveContents = driveContentsResult.getDriveContents();

        FileInputStream fileInputStream = null;
        try {

            fileInputStream = new FileInputStream(DbHelper.databasePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(driveContents.getOutputStream());

        /*reading and writing data to and from file*/
        int n = 0;
        byte[] data = new byte[8 * 1024];
        try {
            while ((n = bufferedInputStream.read(data)) > 0) {

                bufferedOutputStream.write(data, 0, n);
            }

            bufferedOutputStream.flush();
            bufferedOutputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }


        com.google.android.gms.common.api.Status status =
            driveContents.commit(mGoogleApiClient, null).await();
        return status.getStatus().isSuccess();
    }
    return false;
}

尝试上传两个文件时,doInBackground方法仅被调用一次,只有一个文件被上传,这是文件的一个列表中的最后一个文件。

1 个答案:

答案 0 :(得分:0)

您需要发布调用doInBackground的完整代码。问题在于代码的这一部分。

 backupBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            createFolderOnGoogleDrive();
            multipleFilesArrayList = listFilePath();
            createFileAsyncTask = new CreateFileAsyncTask();
            multipleFilestoAsynchTask = multipleFilesArrayList.get(i);
            createFileAsyncTask.execute(multipleFilesArrayList);

        });          }