【发布时间】:2018-09-24 05:39:13
【问题描述】:
我正在尝试将数据(图像或视频)上传到特定的谷歌驱动器。 我希望我的应用程序的用户登录到他们的谷歌帐户,然后从文档中选择特定文件,然后将文件上传到特定的 Google 云端硬盘(不在用户 Google 云端硬盘上)
到目前为止我做了什么?
到目前为止,我的应用程序运行良好。用户使用谷歌帐户登录并选择文件。但是这个文件会转到他们的 Google Drive。 我已经使用 google drive api 搜索了 Upload data to other google drive,但没有找到任何有用的东西。
我想实现什么?
我希望当用户上传数据(视频)时,它会转到特定的 Google 云端硬盘。我有 Google Drive 的 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();
}
});
【问题讨论】:
标签: android google-drive-api google-drive-android-api