Search code examples
androidgoogle-drive-android-api

Why I always get "java.lang.IllegalStateException: DriveContents already closed."


I'm in the process of producing a minimal workable example, to demonstrate the bug described in How to avoid from always loading cached app data from Google Drive

I had completed the code at https://github.com/yccheok/google-drive-bug

The code is intent to either update existing text file, or write a new text file.

However, when testing, I always get exception in driveContents.commit

Status status;
try {
    status = driveContents.commit(googleApiClient, null).await();
} catch (java.lang.IllegalStateException e) {
    // java.lang.IllegalStateException: DriveContents already closed.
    Log.e(TAG, "", e);
    return false;
}

The details of the exception is as follow.

java.lang.IllegalStateException: DriveContents already closed.
      at com.google.android.gms.drive.internal.zzv.zza(Unknown Source)
      at com.google.android.gms.drive.internal.zzv.commit(Unknown Source)
      at org.yccheok.demo.Utils.saveToGoogleDrive(Utils.java:233)
      at org.yccheok.demo.SaveFile123Task.doInBackground(SaveFile123Task.java:13)
      at org.yccheok.demo.SaveFile123Task.doInBackground(SaveFile123Task.java:10)
      at android.os.AsyncTask$2.call(AsyncTask.java:295)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
      at java.lang.Thread.run(Thread.java:818)

I had debugged for a long period, and still have no idea why. I don't think I had missed out any steps.

Correct API key is setup

enter image description here

Steps to reproduce the problem

  1. Click on SAVE 123.TXT" WITH CONTENT "123" button.

enter image description here

enter image description here

enter image description here

Not sure what steps I had missed out, which causes the exception always thrown at this line - https://github.com/yccheok/google-drive-bug/blob/master/app/src/main/java/org/yccheok/demo/Utils.java#L233

Thank you.


Solution

  • According to https://developers.google.com/android/reference/com/google/android/gms/drive/DriveContents

    Once this DriveContents instance has been committed, used for creation, or discarded, it becomes closed and any subsequent method call will throw an IllegalStateException.

    I shouldn't call driveContents.commit after driveFolder.createFile.

    I did a fix via https://github.com/yccheok/google-drive-bug/commit/70919dcfe3fd9108c3af9ecf939a6c0ef03e07cc