Appending data to file using Google Drive Android API

752 Views Asked by At

I am trying to append data to my existing file on Google Drive using the procedure given at https://developers.google.com/drive/android/files#making_modifications

Although control is going correctly but no change is getting reflected in the file.

    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");
                                            }
                                    });
                            }
                    });
    }

Please help me to debug the code.

1

There are 1 best solutions below

0
On BEST ANSWER

Can you try flushing your OutputStream, i.e. writer.flush(), before committing the contents?

Also, instead of reading over all the existing bytes in the InputStream, consider using FileOutputStream#getChannel since FileChannel has facilities to quickly seek to the end of file, i.e. fileChannel.position(fileChannel.size())