Skip to content Skip to sidebar Skip to footer

How To Create Multiple Files Programmatically Using Drive Api For Android?

I am able to create a single file in google drive using CreateFileActivity.java.But i want to upload two or more files to the google drive at a time.How is it possible to create mu

Solution 1:

Assuming, you have multiple files on your Android device, represented by java.io.File objects with known names (titles) and mime type(s):

  java.io.File[] files = {...};
  String[] names[] = {...};
  String[] mimes[] = {...};

You can use this construct to create a set of files in Google drive (in the root, for example):

  com.google.android.gms.common.api.GoogleApiClient mGooApiClient;

  voidfoo() {
    for (inti=0; i < files.length; i++) {
      createFile(null, names[i], mimes[i], files[i]);
    }
  }

   /***********************************************************************
   * create file in GOODrive
   * @param pFldr parent's ID, (null for root)
   * @param titl  file name
   * @param mime  file mime type
   * @param file  java.io.File (with content) to create
   */voidcreateFile(DriveFolder pFldr, final String titl, final String mime, final File file) {
    DriveIddId=null;
    if (mGooApiClient != null && mGooApiClient.isConnected() && titl != null && mime != null && file != null) try {

      finalDriveFolderparent=  pFldr != null ? pFldr : Drive.DriveApi.getRootFolder(mGooApiClient);
      if (pFldr == null) return; //----------------->>>

      Drive.DriveApi.newDriveContents(mGAC).setResultCallback(newResultCallback<DriveContentsResult>() {
        @OverridepublicvoidonResult(DriveContentsResult driveContentsResult) {
          DriveContentscont= driveContentsResult != null && driveContentsResult.getStatus().isSuccess() ?
            driveContentsResult.getDriveContents() : null;
          if (cont != null) try {
            OutputStreamoos= cont.getOutputStream();
            if (oos != null) try {
              InputStreamis=newFileInputStream(file);
              byte[] buf = newbyte[4096];
              int c;
              while ((c = is.read(buf, 0, buf.length)) > 0) {
                oos.write(buf, 0, c);
                oos.flush();
              }
            }
            finally { oos.close();}

            MetadataChangeSetmeta=newBuilder().setTitle(titl).setMimeType(mime).build();

            parent.createFile(mGooApiClient, meta, cont).setResultCallback(newResultCallback<DriveFileResult>() {
              @OverridepublicvoidonResult(DriveFileResult driveFileResult) {
                DriveFiledFil= driveFileResult != null && driveFileResult.getStatus().isSuccess() ?
                                                                      driveFileResult.getDriveFile() : null;
                if (dFil != null) {
                  // save dFil, report success
                } else {
                  // handle error
                }
              }
            });
         } catch (Exception e)  {e.printStackTrace();}
        }
      });
    } catch (Exception e) { e.printStackTrace(); }
  }

Don't forget to save the DriveFile (or DriveId) or the created files.

Good Luck

Post a Comment for "How To Create Multiple Files Programmatically Using Drive Api For Android?"