【问题标题】:Why does OutputStream returned by DriveContents.getOutputStream() not act as expected?为什么 DriveContents.getOutputStream() 返回的 OutputStream 没有按预期运行?
【发布时间】:2017-02-13 00:29:19
【问题描述】:

我一直在使用适用于 Android 的 Google Drive API,但偶然发现了一些问题。特别是我对跨设备保存和同步应用程序数据的应用程序文件夹感兴趣。 我可以查询应用文件夹中是否存在具有特定文件名的文件。 我可以通过

driveFile = metadata.getDriveId().asDriveFile();

我可以通过以下方式打开文件

driveFile.open( mGoogleApiClient,
                DriveFile.MODE_WRITE_ONLY,
                new DownloadProgressListener() {...} )
.setResultCallback(...);

在后面的回调中,我可以得到 OutputStream:

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

但问题是,如果我尝试写入该 OutpustStream,则不会将任何内容写入文件。我用过这样的代码:

OutputStream outputStream = result.getDriveContents().getOutputStream();
OutputStreamWriter writer = new      OutputStreamWriter(outputStream);
try {
    writer.write("Just some strinh I want to save to Google Drive.");
} catch (IOException e) {
    throw new RuntimeException(e);
}
Status status = dcr.getDriveContents().commit(mGoogleApiClient, null).await();

使用此 OutputStream 不会将任何内容写入 GoogleDrive。但是,如果我使用来自 https://developers.google.com/drive/android/files#making_modifications 的代码,它会按预期工作。

从 google 复制的该代码的 sn-p 以供参考:

try {
    ParcelFileDescriptor parcelFileDescriptor = contents.getParcelFileDescriptor();

    FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor
        .getFileDescriptor());
    Writer writer = new OutputStreamWriter(fileOutputStream);
    writer.write("hello world");
} catch (IOException e) {
    e.printStackTrace();
}

为什么 DriveContents.getOutputStream() 可访问的 OutputStream 不能按预期工作?为什么还要提供?还是我错过了什么? Google Play 服务库的版本是 r29。

【问题讨论】:

    标签: android google-drive-android-api


    【解决方案1】:

    关闭 OutputStreamWriter - writer.close() 提交输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      相关资源
      最近更新 更多