Google Drive Android API - Check if folder exists

前端 未结 4 2002
傲寒
傲寒 2020-12-03 14:00

I\'m trying to figure out how to check if a folder exists in Google Drive using the new Google Drive Android API

I\'ve tried the following, thinking that it would ei

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-03 14:35

    After a lot of research this is the code I ended up with. It works properly, but has an issue: When a folder is trashed in Google Drive it takes some time (hours) before the metadata I can fetch from my app is updated, meaning that this code can first detect if the folder has been trashed a couple of hours later the trashing event actually happened - further information and discussions can be found here.

    public class checkFolderActivity extends BaseDemoActivity {
    
        @Override
        public void onConnected(Bundle connectionHint) {
            super.onConnected(connectionHint);
    
            DriveId folderId = DriveId.decodeFromString(folderId);
            DriveFolder folder = Drive.DriveApi.getFolder(mGoogleApiClient, folderId);
            folder.getMetadata(mGoogleApiClient).setResultCallback(metadataRetrievedCallback);
    
        }
    
         final private ResultCallback metadataRetrievedCallback = new
            ResultCallback() {
                @Override
                public void onResult(DriveResource.MetadataResult result) {
                    if (!result.getStatus().isSuccess()) {
                        Log.v(TAG, "Problem while trying to fetch metadata.");
                        return;
                    }
    
                    Metadata metadata = result.getMetadata();
                    if(metadata.isTrashed()){
                        Log.v(TAG, "Folder is trashed");
                    }else{
                        Log.v(TAG, "Folder is not trashed"); 
                    }
    
                }
            };
    }
    

提交回复
热议问题