我一直在使用适用于Android的Google Drive API,但我偶然遇到了一些问题。特别是我对应用程序文件夹感兴趣,以便跨设备保存和同步应用程序数据。 我可以查询app文件夹中是否存在具有特定文件名的文件。 我可以通过
获取文件driveFile = metadata.getDriveId().asDriveFile();
我可以通过
打开文件driveFile.open( mGoogleApiClient,
DriveFile.MODE_WRITE_ONLY,
new DownloadProgressListener() {...} )
.setResultCallback(...);
在后面的回调中,我可以使用:
获取OutputStreamOutputStream 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的该代码的复制片段供参考:
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。