0

I am trying to upload any type of file into google drive using goolge drive api. I have successfully uploaded text files into google drive into root folder of google drive. But i want to create a folder into google drive then i want to upload my file under this folder. 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 ?

1
  • 1
    If you know how to get the MimeType what's the problem with storing it as a variable and using that variable in place of "text/plain" in the line above? Commented Nov 24, 2015 at 7:45

1 Answer 1

1

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).

Good Luck

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.