¿Cómo crear carpetas en Google Drive sin duplicar?

La pregunta es: ¿Cómo crear carpetas en Google Drive sin duplicado?

Estoy respondiendo a la pregunta en este post, y quiero compartir este código con cualquier persona que esté buscando una solución similar o un problema para resolver.

El problema que tuve, es cómo crear carpetas de ruta en la unidad de google sin terminar con carpetas de duplicados en el dirve!

La primera función utilizada para comprobar dónde está la carpeta de su título, pasa la instancia de unidad y el título de la carpeta y los padres Id (no el título):

/** * * @param service google drive instance * @param title the title (name) of the folder (the one you search for) * @param parentId the parent Id of this folder (use root) if the folder is in the main directory of google drive * @return google drive file object * @throws IOException */ private File getExistsFolder(Drive service,String title,String parentId) throws IOException { Drive.Files.List request; request = service.files().list(); String query = "mimeType='application/vnd.google-apps.folder' AND trashed=false AND title='" + title + "' AND '" + parentId + "' in parents"; Logger.info(TAG + ": isFolderExists(): Query= " + query); request = request.setQ(query); FileList files = request.execute(); Logger.info(TAG + ": isFolderExists(): List Size =" + files.getItems().size()); if (files.getItems().size() == 0) //if the size is zero, then the folder doesn't exist return null; else //since google drive allows to have multiple folders with the same title (name) //we select the first file in the list to return return files.getItems().get(0); } 

La función utilizada para crear una carpeta dentro de las referencias de padres givien, si la lista está vacía, entonces la carpeta se creará en el directorio raíz de la unidad google.

 /** * * @param service google drive instance * @param title the folder's title * @param listParentReference the list of parents references where you want the folder to be created, * if you have more than one parent references, then a folder will be created in each one of them * @return google drive file object * @throws IOException */ private File createFolder(Drive service,String title,List<ParentReference> listParentReference) throws IOException { File body = new File(); body.setTitle(title); body.setParents(listParentReference); body.setMimeType("application/vnd.google-apps.folder"); File file = service.files().insert(body).execute(); return file; } 

La tercera función utilizada para crear una ruta de directorio de las carpetas sin duplicados en la unidad de google. Con el fin de evitar las carpetas duplicadas en la unidad google, la función comprobará si la carpeta existe o no antes de crearla.

 /** * * @param service google drive instance * @param titles list of folders titles * ie if your path like this folder1/folder2/folder3 then pass them in this order createFoldersPath(service, folder1, folder2, folder3) * @return parent reference of the last added folder in case you want to use it to create a file inside this folder. * @throws IOException */ private List<ParentReference> createFoldersPath(Drive service,String...titles) throws IOException { List<ParentReference> listParentReference = new ArrayList<ParentReference>(); File file = null; for(int i=0;i<titles.length;i++) { file = getExistsFolder(service, titles[i], (file==null)?"root":file.getId()); if (file == null) { file = createFolder(service, titles[i], listParentReference); } listParentReference.clear(); listParentReference.add(new ParentReference().setId(file.getId())); } return listParentReference; } 
  • Cómo mostrar sólo la hora actual utilizando DateFormat.getDateTimeInstance () en android
  • ¿Cómo puedo implementar un AsyncTaskLoader simple?
  • ¿Dónde están todos los recursos estándar de iconos de Android?
  • ¿Qué lenguajes de programación se pueden utilizar para desarrollar en Android?
  • Obtener dirección MAC Ethernet de Android (no interfaz wifi)
  • Android SimpleDateFormat, cómo usarlo?
  • ¿Qué hay de malo en este método de cálculo inSampleSize para Bitmaps? Sin memoria
  • Android: ¿Cómo usar la clase de gestor de descargas?
  • La mejor manera de comparar fechas en Android
  • Cómo serializar para la clase android.location?
  • Cargar un TextView desde xml en un TextSwitcher
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.