Reputation: 1463
I have a weird bug I'm hoping someone can help me with.
I am using the Google Drive app folder to backup some data from my app. It creates the file and writes my data without and issue, I can sign grab the file from drive at any time and it works fine.
However if I uninstall the app when I reinstall it the Google Drive App folder is empty. I'm assuming I'm missing some point of the sync process so it's only caching locally but I can't see what I've done wrong. My code to create and commit the file is below.
DriveFile file = ..//Get drive file;
file.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, null)
.setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
Log.i(TAG, "Problem opening file, status is " + result.getStatus().toString());
return;
}
Log.i(TAG, "File opened");
writeFileContents(result.getDriveContents());
}
});
private void writeFileContents(final DriveContents contents) {
new AsyncTask<Object, Object, Integer>() {
@Override
protected Integer doInBackground(Object[] params) {
try {
ParcelFileDescriptor parcelFileDescriptor = contents.getParcelFileDescriptor();
// Overwrite the file.
FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor
.getFileDescriptor());
Writer writer = new OutputStreamWriter(fileOutputStream);
SyncUtils.writeXml("some xml", writer);
writer.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return;
}
@Override
protected void onPostExecute(final Integer x) {
contents.commit(mGoogleApiClient, null).setResultCallback(new ResultCallback<com.google.android.gms.common.api.Status>() {
@Override
public void onResult(com.google.android.gms.common.api.Status result) {
if(result.getStatus().isSuccess())
Log.i(TAG, "Write succeeded");
else
Log.i(TAG, "Write failed");
onConnectionCallbacks.onSynced(x);
}
});
}
}.execute();
Upvotes: 0
Views: 487
Reputation: 4905
According to the documentation, contents.getParcelFileDescriptor()
only works with DriveFile.MODE_READ_WRITE
. You are using DriveFile.MODE_WRITE_ONLY
, so you should be calling contents.getOutputStream()
.
Upvotes: 0