这里我尝试在 Google 云端硬盘应用文件夹上上传多个文件:
@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
方法仅被调用一次,并且仅上传一个文件,该文件位于文件数组列表中的最后一个。
最佳答案
您需要发布调用 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);
}); }
关于java - 将多个文件上传到 Google 云端硬盘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41647368/