The problem I am facing is how to create a folder path in Google Drive without ending up with duplicate copies in the dirve directory!
The first function used to check where the folder exists from its name transfers the instance of your disk and the title of the folder, and the parent identifier (not the title):
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)
function used to create a folder inside parental parent links; if the list is empty, the folder will be created in the root directory of the Google root directory.
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; }
The third function used to create a directory path to folders without duplicates on a Google drive. to prevent duplicate folders on the Google drive, the function will check if the folder exists or not before it is created.
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; }
moh.sukhni
source share