代码之家  ›  专栏  ›  技术社区  ›  Ali Ahmed

将数据上载到特定的google驱动器帐户

  •  -2
  • Ali Ahmed  · 技术社区  · 7 年前

    我正在尝试上传数据(图像或视频)到特定的谷歌驱动器。 (不在用户Google Drive上)

    到目前为止我做了什么?

    到目前为止,我的应用程序运行良好。用户使用google帐户登录并选择文件。但这个文件会被放到他们的谷歌硬盘上。 我已经使用google drive api搜索了上传到其他google驱动器的数据,但是没有发现任何有用的东西。

    我想当用户上传数据(视频)到特定的谷歌驱动器。我有谷歌硬盘的App_键,我想在上面上传数据。

    附上我的登录和上传数据的代码。

    谢谢您。

    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();
                    }
                });
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Pay    7 年前

    如果你想把它发送到另一个谷歌驱动器帐户,我想你可以使用的是 Google's Team Drives

    然后,您可以添加当前作为团队一部分登录的用户。然后他们可以共享并上传到特定的团队驱动器。

    我希望有帮助。

    推荐文章