2013-08-09 11 views
7

सवाल यह है कि: बिना किसी डुप्लिकेट के Google ड्राइव में फ़ोल्डर्स कैसे बनाएं?डुप्लिकेट के बिना Google ड्राइव में फ़ोल्डर कैसे बनाएं?

मैं इस पोस्ट में सवाल का जवाब दे रहा हूं, और मैं इस कोड को किसी भी ऐसे व्यक्ति के साथ साझा करना चाहता हूं जो हल करने के लिए एक समान समाधान या समस्या की तलाश कर रहा हो।

+7

यदि आप हमारे साथ ज्ञान साझा करना चाहते हैं, तो आप केवल अपनी प्रश्न को संपादित करने के लिए अपनी पोस्ट संपादित कर सकते हैं और बाकी को उत्तर के रूप में पोस्ट कर सकते हैं। आप अपने स्वयं के प्रश्न का उत्तर दे सकते हैं और यह लोगों को समझने के लिए इस तरह की पोस्ट को आसान बनाता है :) –

उत्तर

4

मेरे पास जो समस्या थी, वह है कि कैसे Google ड्राइव में फ़ोल्डर्स पथ बनाने के लिए डुबकी में डुप्लीकेट फ़ोल्डरों के साथ समाप्त हो गया है!

/** 
    * 
    * @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); 
    } 

समारोह के लिए इस्तेमाल किया:

पहले जहां फ़ोल्डर इसके शीर्षक से मौजूद है की जाँच करने के लिए इस्तेमाल समारोह, अपने ड्राइव उदाहरण और फ़ोल्डर के शीर्षक और यह के माता-पिता आईडी (शीर्षक नहीं) पास अगर सूची खाली है, तो जिवियन माता-पिता संदर्भों के अंदर एक फ़ोल्डर बनाएं, तो फ़ोल्डर 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; 

} 

तीसरा फ़ंक्शन Google ड्राइव में डुप्लिकेट किए बिना फ़ोल्डरों का निर्देशिका पथ बनाने के लिए उपयोग किया जाता है। Google ड्राइव में डुप्लिकेट फ़ोल्डर को रोकने के लिए, फ़ंक्शन यह जांच करेगा कि फ़ोल्डर मौजूद है या नहीं, इसे बनाने से पहले।

/** 
* 
* @param service google drive instance 
* @param titles list of folders titles 
* i.e. 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; 
} 
+0

आप किस प्रकार का एपीआई उपयोग कर रहे हैं? :) आधिकारिक Google ड्राइव एंड्रॉइड एसडीके (https://developers.google.com/drive/android/get-started) की तुलना में उपयोग करना बहुत आसान लगता है – qkx

+0

इसे अब पुराना उत्तर माना जाता है, नया Google ड्राइव एपीआई अलग-अलग तरीका है फ़ाइलों से निपटने के लिए, दुख की बात है कि पुरानी एपीआई का उपयोग करने की अनुशंसा नहीं की जाती है :( –

+0

आप कमाल हैं! –

संबंधित मुद्दे