2013-08-09 17 views
7

câu hỏi là: Cách tạo thư mục trong ổ google mà không cần sao chép?Cách tạo thư mục trong ổ google mà không bị trùng lặp?

Tôi đang trả lời câu hỏi trong bài đăng này và tôi muốn chia sẻ mã này với bất kỳ ai đang tìm kiếm giải pháp hoặc vấn đề tương tự để giải quyết.

+7

Nếu bạn muốn chia sẻ kiến ​​thức với chúng tôi, bạn có thể chỉnh sửa bài đăng của mình để chỉ chứa câu hỏi và sau đó đăng phần còn lại làm câu trả lời. Bạn có thể trả lời câu hỏi của riêng mình và làm cho các bài đăng như thế này dễ hiểu hơn cho mọi người :) –

Trả lời

4

vấn đề mà tôi có, là cách tạo đường dẫn thư mục trong ổ google mà không cần kết thúc với các thư mục trùng lặp trong dirve!

chức năng đầu tiên sử dụng để kiểm tra nơi thư mục được tồn tại từ tiêu đề của nó, vượt qua ví dụ của bạn ổ và tiêu đề của thư mục và cha mẹ của nó Id (không phải là tiêu đề):

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

chức năng sử dụng để tạo một thư mục bên trong tài liệu tham khảo cha mẹ givien, nếu danh sách trống, thì thư mục sẽ được tạo trong thư mục gốc của ổ đĩa 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; 

} 

chức năng thứ ba được sử dụng để tạo đường dẫn thư mục của các thư mục không trùng lặp trong ổ google. để ngăn các thư mục trùng lặp trong ổ google, chức năng sẽ kiểm tra xem thư mục có tồn tại hay không trước khi tạo thư mục đó.

/** 
* 
* @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

bạn đang sử dụng loại api nào? :) có vẻ dễ sử dụng hơn SDK Android Google Drive chính thức (https://developers.google.com/drive/android/get-started) – qkx

+0

Điều này hiện được coi là câu trả lời lỗi thời, API ổ google mới có cách khác để xử lý các tệp, thật đáng tiếc là không nên sử dụng API cũ: ( –

+0

bạn thật tuyệt vời! –

Các vấn đề liên quan