使用 Google 驱动器 Android API 将数据附加到文件
Appending data to file using Google Drive Android API
我正在尝试使用 https://developers.google.com/drive/android/files#making_modifications
中给出的程序将数据附加到 Google 驱动器上的现有文件
虽然控制正常,但文件中没有反映任何变化。
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,即 writer.flush()?
此外,考虑使用 FileOutputStream#getChannel 而不是读取 InputStream 中的所有现有字节,因为 FileChannel 具有快速查找文件末尾的功能,即 fileChannel.position(fileChannel.size( ))
我正在尝试使用 https://developers.google.com/drive/android/files#making_modifications
中给出的程序将数据附加到 Google 驱动器上的现有文件虽然控制正常,但文件中没有反映任何变化。
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,即 writer.flush()?
此外,考虑使用 FileOutputStream#getChannel 而不是读取 InputStream 中的所有现有字节,因为 FileChannel 具有快速查找文件末尾的功能,即 fileChannel.position(fileChannel.size( ))