2015-11-24 133 views
0

我想使用谷歌驅動器api將任何類型的文件上傳到谷歌驅動器。 我已經成功地將文本文件上傳到谷歌驅動器到谷歌驅動器的根文件夾。 但我想創建一個文件夾到谷歌驅動器,然後我想上傳我的文件在這個文件夾下。 我用下面的代碼上傳文本文件:在android中使用谷歌驅動器api上傳文件

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); 
      } 

現在怎麼可以創建一個文件夾,然後上傳我的文件到該文件夾​​?

有沒有人有任何解決方案?

+1

如果你知道如何獲得Mime類型有什麼用把它作爲一個變量,並在上面的行使用變量來替代「text/plain的」的問題呢? – db579

回答

1

創建一個文件夾,然後創建一個文件夾作爲它的父文件夾。

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; //--------------------->>> 
    } 

您需要在非UI線程(的AsyncTask)運行它,或者改變等待()■setResultCallback()■(從here拍攝)。

好運

相關問題