将文本文件从移动存储升级到Google驱动器文件后,显示Google驱动器中的零字节

时间:2018-12-14 17:32:00

标签: google-drive-api google-drive-android-api

为什么将文本文件从移动存储升级到显示在Google驱动器中为零字节的Google驱动器文件之后?

我的代码:

private void create_file_in_folder(final DriveId driveId) {

Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
  @Override public void onResult(@NonNull DriveApi.DriveContentsResult driveContentsResult) {
    if (!driveContentsResult.getStatus().isSuccess()) {
      Log.e(TAG, "U AR A MORON! Error while trying to create new file contents");
      return;
    }

    OutputStream outputStream = driveContentsResult.getDriveContents().getOutputStream();


    final File theFile = new File("/storage/emulated/0/notepad/note.txt"); 
    try {
      FileInputStream fileInputStream = new FileInputStream(theFile);
      byte[] buffer = new byte[2048];
      int bytesRead;
      while ((bytesRead = fileInputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
      }
    } catch (IOException e1) {
      Log.i(TAG, "U AR A MORON! Unable to write file contents.");
    }

    MetadataChangeSet changeSet = new MetadataChangeSet.Builder().setTitle(theFile.getName()).setMimeType("text/plain").setStarred(false).build();
    DriveFolder folder = driveId.asDriveFolder();
    folder.createFile(mGoogleApiClient, changeSet, driveContentsResult.getDriveContents())
        .setResultCallback(new ResultCallback<DriveFolder.DriveFileResult>() {
          @Override public void onResult(@NonNull DriveFolder.DriveFileResult driveFileResult) {
            if (!driveFileResult.getStatus().isSuccess()) {
              Log.e(TAG, "U AR A MORON!  Error while trying to create the file");
              return;
            }
            Log.v(TAG, "Created a file: " + driveFileResult.getDriveFile().getDriveId());
          }
        });
  }
});
}

0 个答案:

没有答案