2017-07-05 104 views
0

我面臨的問題在創建一個文件夾內的文件,我創建了單獨的文件和單獨的文件夾在谷歌驅動器,但我不能在谷歌驅動器中的文件夾內創建文件如何在谷歌驅動器中的文件夾內創建文件?

我已經用於創建在谷歌驅動器中的文件夾中的文件被髮送下面

public void onClickFolderInRootFlder(View view) 
{ 

    MetadataChangeSet changeSet = new MetadataChangeSet.Builder().setTitle("New CallLog folders").build(); 
    Drive.DriveApi.getRootFolder(mGoogleApiClient).createFolder(
      mGoogleApiClient, changeSet).setResultCallback(callback); 
} 

final ResultCallback<DriveFolder.DriveFolderResult> callback = new ResultCallback<DriveFolder.DriveFolderResult>() { 
    @Override 
    public void onResult(DriveFolder.DriveFolderResult result) { 
     if (!result.getStatus().isSuccess()) { 
      Toast.makeText(MainActivity.this,"Error while trying to create the folder",Toast.LENGTH_SHORT).show(); 
      return; 
     } 
     Toast.makeText(MainActivity.this,"Created a folder with content: " + 
       result.getDriveFolder().getDriveId(),Toast.LENGTH_SHORT).show(); 
     Drive.DriveApi.fetchDriveId(mGoogleApiClient, String.valueOf(result.getDriveFolder().getDriveId())) 
       .setResultCallback(idCallback); 
    } 
}; 

final private ResultCallback<DriveApi.DriveIdResult> idCallback = new ResultCallback<DriveApi.DriveIdResult>() { 
    @Override 
    public void onResult(DriveApi.DriveIdResult result) { 
     if (!result.getStatus().isSuccess()) { 
     //  showMessage("Cannot find DriveId. Are you authorized to view this file?"); 
      Toast.makeText(MainActivity.this,"Cannot find DriveId. Are you authorized to view this file?",Toast.LENGTH_SHORT).show(); 
      return; 
     } 
    DriveId mFolderDriveId = result.getDriveId(); 
     Drive.DriveApi.newDriveContents(mGoogleApiClient) 
       .setResultCallback(driveContentsCallback); 
    } 
}; 

final ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback = 
     new ResultCallback<DriveApi.DriveContentsResult>() { 
      @Override 
      public void onResult(DriveApi.DriveContentsResult result) { 
       if (!result.getStatus().isSuccess()) { 
        // display an error saying file can't be opened 
        return; 
       } 

       else{ 
        CreateFileOnGoogleDrive(result); 
       } 
       // DriveContents object contains pointers 
       // to the actual byte stream 
       DriveContents contents = result.getDriveContents(); 
      } 


     }; 


public void CreateFileOnGoogleDrive(DriveApi.DriveContentsResult result){ 

    final DriveContents driveContents = result.getDriveContents(); 

    // Perform I/O off the UI thread. 
    new Thread() { 
     @Override 
     public void run() { 
      // write content to DriveContents 
      OutputStream outputStream = driveContents.getOutputStream(); 
      Writer writer = new OutputStreamWriter(outputStream); 
      try { 
       writer.write("Hello World"); 
       writer.close(); 

      } catch (IOException e) { 
       Log.e(TAG, e.getMessage()); 
      } 

      MetadataChangeSet changeSet = new MetadataChangeSet.Builder() 
        .setTitle("prabha") 
        .setMimeType("text/plain") 
        .setStarred(true).build(); 

      // create a file in root folder 
      Drive.DriveApi.getRootFolder(mGoogleApiClient) 
        .createFile(mGoogleApiClient, changeSet, driveContents).setResultCallback(fileCallback); 
     } 
    }.start(); 
} 
final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new 
     ResultCallback<DriveFolder.DriveFileResult>() { 
      @Override 
      public void onResult(DriveFolder.DriveFileResult result) { 
       if (!result.getStatus().isSuccess()) { 
        Toast.makeText(MainActivity.this,"Error while trying to create the file",Toast.LENGTH_SHORT).show(); 
        return; 
       } 
       Toast.makeText(MainActivity.this,"Created a file with content: " + 
         result.getDriveFile().getDriveId(),Toast.LENGTH_SHORT).show(); 
      } 
     }; 

這個方法我試過,但問題是發生在線路

final private ResultCallback<DriveApi.DriveIdResult> idCallback = new ResultCallback<DriveApi.DriveIdResult>() { 
    @Override 
    public void onResult(DriveApi.DriveIdResult result) { 
     if (!result.getStatus().isSuccess()) { 
     //  showMessage("Cannot find DriveId. Are you authorized to view this file?"); 
      Toast.makeText(MainActivity.this,"Cannot find DriveId. Are you authorized to view this file?",Toast.LENGTH_SHORT).show(); 
      return; 
     } 
    DriveId mFolderDriveId = result.getDriveId(); 
     Drive.DriveApi.newDriveContents(mGoogleApiClient) 
       .setResultCallback(driveContentsCallback); 
    } 
}; 

的result.getStatus.isSuccess是假的。請返回幫助我如何在谷歌驅動器中的文件夾內創建文件。

回答

0

要調用

Drive.DriveApi.getRootFolder(mGoogleApiClient).createFile 

它(不出所料)創建的根文件夾中的文件。 相反,你應該叫

DriveFolder.createFile 

截至https://developers.google.com/android/reference/com/google/android/gms/drive/DriveFolder.html#createFile(com.google.android.gms.common.api.GoogleApiClient,com.google.android.gms.drive.MetadataChangeSet,com.google.android.gms.drive.DriveContents)

+0

描述,你會回答這個問題https://stackoverflow.com/questions/44941476/how-to-get-the-drive-id-for-創建文件夾在谷歌驅動器 –

+1

行 - 你接受這個答案後。這就是StackOverflow的工作原理。 – pinoyyid

相關問題