简体   繁体   English

在Android中使用Google Drive API上传文件

[英]Uploading files using google drive api in android

I am trying to upload any type of file into google drive using goolge drive api. 我正在尝试使用goolge驱动器api将任何类型的文件上传到Google驱动器。 I have successfully uploaded text files into google drive into root folder of google drive. 我已成功将文本文件上传到Google驱动器中,并上传到Google驱动器的根文件夹中。 But i want to create a folder into google drive then i want to upload my file under this folder. 但是我想在Google驱动器中创建一个文件夹,然后在该文件夹下上传我的文件。 I used below code to upload a text file: 我使用下面的代码上传文本文件:

    public void run() {
                OutputStream outputStream = driveContents.getOutputStream();
                addTextfileToOutputStream(outputStream);
                MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                        .setTitle(uploadFileName)
                        .setMimeType("text/plain")
                        .setDescription(
                                "")
                        .setStarred(true).build();

                Drive.DriveApi
                        .getRootFolder(googleApiClient)             
                        .createFile(googleApiClient, changeSet,
                                driveContents)
                        .setResultCallback(fileCallback);
            }

Now how can create a folder and then upload my file into this folder? 现在如何创建一个文件夹,然后将我的文件上传到该文件夹​​?

Is there anyone who have any solution ? 有谁有什么解决方案?

Create a folder, and then create a file with the folder as it's parent. 创建一个文件夹,然后使用该文件夹作为父文件夹创建一个文件。

  private static GoogleApiClient mGAC;
  //...

  /*********************************************************************
   * create file/folder in GOODrive
   * @param parentFolder parent folder, null for root
   * @param titl folder name
   * @return folder object
   */
  static DriveFolder createFolder(DriveFolder parentFolder, String titl) {
    DriveId dId = null;
    if (mGAC != null && mGAC.isConnected() && titl != null) try {
      if (parentFolder == null)
        parentFolder =  Drive.DriveApi.getRootFolder(mGAC);
      if (parentFolder == null) return null; //----------------->>>

      MetadataChangeSet meta;
      meta = new Builder().setTitle(titl)
                        .setMimeType("application/vnd.google-apps.folder").build();
      DriveFolderResult r1 = parentFolder.createFolder(mGAC, meta).await();
      DriveFolder dFld =
        (r1 != null) && r1.getStatus().isSuccess() ? r1.getDriveFolder() : null;
      if (dFld != null) {
        MetadataResult r2 = dFld.getMetadata(mGAC).await();
        if ((r2 != null) && r2.getStatus().isSuccess()) {
          dId = r2.getMetadata().getDriveId();
        }
      }
    } catch (Exception e) { e.printStackTrace(); }
    return dId == null ? null : dId.asDriveFolder();
  }
  /********************************************************************
   * create file in GOODrive
   * @param pFldr parent's ID, null for root
   * @param titl  file name
   * @param mime  file mime type
   * @param file  file (with content) to create
   * @return file object
   */
  static DriveFile createFile(DriveFolder pFldr, String titl, String mime, File file) {
    DriveId dId = null;
    if (mGAC != null && mGAC.isConnected() && titl != null && mime != null && file != null) try {
      if (pFldr == null)
        pFldr =  Drive.DriveApi.getRootFolder(mGAC);
      if (pFldr == null) return null; //----------------->>>

      DriveContents cont = file2Cont(null, file);
      MetadataChangeSet meta = new Builder().setTitle(titl).setMimeType(mime).build();
      DriveFileResult r1 = pFldr.createFile(mGAC, meta, cont).await();
      DriveFile dFil = r1 != null && r1.getStatus().isSuccess() ? r1.getDriveFile() : null;
      if (dFil != null) {
        MetadataResult r2 = dFil.getMetadata(mGAC).await();
        if (r2 != null && r2.getStatus().isSuccess()) {
          dId = r2.getMetadata().getDriveId();
        }
      }
    } catch (Exception e) { e.printStackTrace(); }
    return dId == null ? null : dId.asDriveFile();
  }

  private static DriveContents file2Cont(DriveContents cont, File file) {
    if (file == null) return null;  //--------------------->>>
    if (cont == null) {
      DriveContentsResult r1 = Drive.DriveApi.newDriveContents(mGAC).await();
      cont = r1 != null && r1.getStatus().isSuccess() ? r1.getDriveContents() : null;
    }
    if (cont != null) try {
      OutputStream oos = cont.getOutputStream();
      if (oos != null) try {
        InputStream is = new FileInputStream(file);
        byte[] buf = new byte[4096];
        int c;
        while ((c = is.read(buf, 0, buf.length)) > 0) {
          oos.write(buf, 0, c);
          oos.flush();
        }
      }
      finally { oos.close();}
      return cont; //++++++++++++++++++++++++++++++>>>
    } catch (Exception ignore)  {}
    return null;   //--------------------->>>
  }

You need to run it in non-UI thread (AsyncTask), or change await()s into setResultCallback()s (taken from here ). 您需要在非UI线程(AsyncTask)中运行它,或将await()更改为setResultCallback() (从此处获取 )。

Good Luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM