Sriram
Sriram

Reputation: 389

Android Drive API - Checking if a file exists always indicates not existing

I'm using this piece of code to check whether a file exists in Drive:

Query query = new Query.Builder()
                        .addFilter(Filters.eq(SearchableField.TITLE, "tmp.txt"))
                        .build();
new MyAsyncTask().execute(query);

class MyAsyncTask extends AsyncTask<Query, Void, MetadataBufferResult>{
    @Override
    protected MetadataBufferResult doInBackground(Query... params) {
        return Drive.DriveApi.query(getGoogleApiClient(), params[0]).await();
}

   @Override
   protected void onPostExecute(MetadataBufferResult metadataBufferResult) {
       if(metadataBufferResult.getMetadataBuffer().getCount()==0)
           showMessage("File does not exist");
       else
           showMessage("File exists");
    }
}

My logic basically is to use a Query to filter out all files with name "tmp.txt" and then check if the count of the thus returned MetaDataBuffer is not 0. However, whether or not the file exists, the count is always 0 and the message is displayed as "File does not exist". What am I missing here? I'm using a modified version of QueryFilesActivity from the android-demo-master provided by Android.

Upvotes: 1

Views: 1045

Answers (1)

Shailendra
Shailendra

Reputation: 213

There are 3 reasons why the query returns 0 items:

  1. The item does not exist.
  2. The item exists, but your app doesn't have access to that item.
  3. The item exists (on server), your app has access to that item but the item is not synced locally (device is offline, or cache is not up-to-date). In this particular case your could call the requestSync method to ensure the local cache is updated and query again.

Upvotes: 1

Related Questions