我正试图使用中给出的过程将数据附加到Google Drive上的现有文件中https://developers.google.com/drive/android/files#making_modifications
虽然控制正在正常进行,但没有任何更改反映在文件中。
public void addHistoryEntry(final String location) {
final DriveFile file = Drive.DriveApi.getFile(getClient(), historyFileId);
file.open(getClient(), DriveFile.MODE_READ_WRITE, null)
.setResultCallback(new ResultCallback<DriveContentsResult>() {
@Override
public void onResult(DriveContentsResult driveContentsResult) {
if (!driveContentsResult.getStatus().isSuccess()) {
L.c("Problem in Writing to file");
return;
}
DriveContents contents = driveContentsResult.getDriveContents();
try {
ParcelFileDescriptor parcelFileDescriptor = contents.getParcelFileDescriptor();
FileInputStream fileInputStream = new FileInputStream(parcelFileDescriptor.getFileDescriptor());
// Read to the end of the file.
fileInputStream.read(new byte[fileInputStream.available()]);
// Append to the file.
FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor
.getFileDescriptor());
Writer writer = new OutputStreamWriter(fileOutputStream);
writer.write(location+"n");
} catch (IOException e) {
e.printStackTrace();
}
contents.commit(getClient(), null).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
if(status.isSuccess()) L.c("Write to file successful");
else L.c("Write to file failed");
}
});
}
});
}
请帮我调试代码。
在提交内容之前,是否可以尝试刷新OutputStream,即writter.flush()?
此外,与其读取InputStream中的所有现有字节,不如考虑使用FileOutputStream#getChannel,因为FileChannel具有快速查找文件末尾的功能,即FileChannel.position(FileChannel.size())