0
votes

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();
1
How did you configure your authentication? I've had a similar problem and was told that I used the wrong type of authentication... - fge
Hmmm.. Is this what you mean? mGoogleApiClient = new GoogleApiClient.Builder(context) .addApi(Drive.API) .addScope(Drive.SCOPE_APPFOLDER) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); - crazyfool
Argh, that is yet another way to initialize a client which I don't know about. Meh. Google's APIs are waaay too complicated... - fge
There was an issue in a previous version of Play Services where application data wasn't syncing properly after app re-installation. If you are not using Play Services 6.5 or above, please give that a try. Otherwise, could you create a bug against the Drive Android API on the issue tracker? code.google.com/a/google.com/p/apps-api-issues - Daniel

1 Answers

0
votes

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().