我正在尝试将数据(图像或视频)上传到特定的Google驱动器。 我希望我的应用程序的用户登录其Google帐户,然后从“文档”中选择特定文件,然后将文件上传到特定的Google云端硬盘(不在用户Google云端硬盘上)
到目前为止我做了什么?
到目前为止,我的应用程序运行正常。用户使用Google帐户登录并选择文件。但是此文件将转到他们的Google云端硬盘。 我已经使用Google Drive API搜索了“将数据上传到其他Google Drive”,但是找不到有用的东西。
我想要实现什么?
我希望用户上传数据(视频)时将其转到特定的Google云端硬盘。我有要在上面上传数据的Google云端硬盘的App_Key。
我的登录和上传数据代码已随附。
谢谢。
登录代码
GoogleSignInClient googleSignInClient = buildGoogleSignInClient();
startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE);
private GoogleSignInClient buildGoogleSignInClient() {
GoogleSignInOptions signInOptions = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE)
.build();
return GoogleSignIn.getClient(this, signInOptions);
}
使用以下文件上传文件:
final Task<DriveFolder> rootFolderTask = mDriveResourceClient.getRootFolder();
final Task<DriveContents> createContentsTask = mDriveResourceClient.createContents();
Tasks.whenAll(rootFolderTask, createContentsTask)
.continueWithTask(new Continuation<Void, Task<DriveFile>>() {
@Override
public Task<DriveFile> then(@NonNull Task<Void> task) throws Exception {
DriveFolder parent = rootFolderTask.getResult();
DriveContents contents = createContentsTask.getResult();
File file = new File(uri.toString());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
FileInputStream fis = new FileInputStream(file);
for (int readNum; (readNum = fis.read(buf)) != -1;) {
baos.write(buf, 0, readNum);
}
OutputStream outputStream = contents.getOutputStream();
outputStream.write(baos.toByteArray());
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("MyVideo.mp4") // Provide you video name here
.setMimeType("video/mp4") // Provide you video type here
.build();
return mDriveResourceClient.createFile(parent, changeSet, contents);
}
})
.addOnSuccessListener(this,
new OnSuccessListener<DriveFile>() {
@Override
public void onSuccess(DriveFile driveFile) {
Toast.makeText(MainActivity.this, "Upload Started", Toast.LENGTH_SHORT).show();
}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Unable to create file", e);
Toast.makeText(MainActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
答案 0 :(得分:0)
如果您想将其发送到另一个Google云端硬盘帐户,我认为您可以使用Google's Team Drives
团队驱动器是一个共享的空间,团队可以在其中轻松地通过任何设备在任何地方存储,搜索和访问其文件。 然后,您可以添加当前登录的用户作为您的团队的一部分。然后,他们可以共享并上传到该特定团队。
我希望它会有所帮助。