【问题标题】:upload data to specific google drive account将数据上传到特定的 Google Drive 帐户
【发布时间】: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


    【解决方案1】:

    如果您想将其发送到另一个 google drive 帐户,我认为您可以使用 Google's Team Drives

    Team Drives 是一个共享空间,团队可以在其中通过任何设备轻松存储、搜索和访问他们的文件。 然后,您可以将当前登录的用户添加为团队的一部分。然后他们可以共享并上传到该特定的团队驱动器。

    我希望它有所帮助。

    【讨论】:

    • 没有。我不想要这个。在团队云端硬盘中,Google 云端硬盘帐户持有人必须先添加团队中的成员。我想要的是:将 Google Drive 帐户持有者的帐户添加到我的应用程序中,使用其帐户登录的任何人都可以将数据上传到该帐户,
    • 你可以称之为共享文件夹的东西。因此,每个人都可以访问该文件夹,并且每个人都可以直接将数据上传到该文件夹​​。未经帐户持有人许可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 1970-01-01
    相关资源
    最近更新 更多