Google Drive Android API - Check if folder exists

前端 未结 4 1998
傲寒
傲寒 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:36

    So today the answer is out of date API. So I have posted example of how to check the folder if exists with the new update of documentation:

         fun checkFolder(name: String):Boolean {
             check(googleDriveService != null) { "You have to init Google Drive Service first!" }
             return search(name, FOLDER_MIME_TYPE)
         }
        
         private fun search(name: String, mimeType:String): Boolean {
        
                var pageToken: String? = null
        
                do {
        
                    val result: FileList =
                        googleDriveService!!
                            .files()
                            .list()
                            .setQ("mimeType='$FOLDER_MIME_TYPE'")
                            .setSpaces("drive")
                            .setFields("nextPageToken, files(id, name)")
                            .setPageToken(pageToken)
                            .execute()
        
                    for (file in result.files) {
                        Log.d(TAG_UPLOAD_FILE , "Found file: %s (%s)\n ${file.name}, ${file.id} ")
                        if (name == file.name) return true
                    }
        
                    pageToken = result.nextPageToken
        
                } while (pageToken != null)
        
                return false
            }
    
    private const val FOLDER_MIME_TYPE= "application/vnd.google-apps.folder"
    

提交回复
热议问题