我正在尝试使用GoogleDriveApi在文件夹内创建文件。创建文件夹后,可以直接使用DriveFolder
中的SuccessListener
在其中创建文件。但是我不知道以后如何在其中创建文件...因为dF.getDriveId().encodeToString()
不起作用,所以以后不能再创建DriveFolder对象。
CreateFolder:
Drive.getDriveResourceClient(getActivity(), gsa)
.getRootFolder()
.continueWithTask(new Continuation<DriveFolder, Task<DriveFolder>>() {
@Override
public Task<DriveFolder> then(@NonNull Task<DriveFolder> task) throws Exception {
DriveFolder parentFolder = task.getResult();
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("Foo")
.setMimeType(DriveFolder.MIME_TYPE)
.setStarred(true)
.build();
return Drive.getDriveResourceClient(getActivity(), gsa).createFolder(parentFolder, changeSet);
}
})
.addOnSuccessListener(new OnSuccessListener<DriveFolder>() {
@Override
public void onSuccess(DriveFolder dF){
//Here I get the DriverFolder Object.
driveFolder = df;
}
});
CreateFile:
final Task<DriveFolder> rootFolderTask = Drive.getDriveResourceClient(getActivity(), gsa).getRootFolder();
final Task<DriveContents> createContentsTask = Drive.getDriveResourceClient(getActivity(), gsa).createContents();
Tasks.whenAll(rootFolderTask, createContentsTask)
.continueWithTask(new Continuation<Void, Task<DriveFile>>() {
@Override
public Task<DriveFile> then(@NonNull Task<Void> task) throws Exception {
DriveContents contents = createContentsTask.getResult();
OutputStream outputStream = contents.getOutputStream();
ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
BitmapFactory.decodeFile("path").compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
try {
outputStream.write(bitmapStream.toByteArray());
} catch (IOException e1) {
Log.i("ERROR", "Unable to write file contents.");
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("Foo.png")
.setMimeType("image/png")
.setStarred(true)
.build();
//use driveFolder from CreateFolder
return Drive.getDriveResourceClient(getActivity(), gsa).createFile(driveFolder, changeSet, contents)
}
});
答案 0 :(得分:0)
我用Drive.getDriveClient(this, gsa).newOpenFileActivityIntentSender
打开FilePicker解决了问题,并让用户选择了该文件夹。在onActivityResult
中,我得到data.getExtras().get("response_drive_id")
的DriveId。使用Drive.getDriveClient(this, gsa).getDriveId(driveId.getResourceId)
,我可以重新创建DriveID对象并将文件上传到该对象。而已。
但是,在创建文件夹之后,我仍然想知道如何在driveFolder.getDriveId().getResourceId()
中获得onSuccess
。