[英]Save file in specific folder with Google Drive SDK
我一直在嘗試將純文本文件保存到Android上的Google雲端硬盤中的特定文件夾中。
到目前為止,使用Google雲端硬盤上的文檔和快速入門指南 ,我已經能夠做一些正確的方向,首先我能夠創建一個純文本文件:
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
File file = service.files().insert(body, textContent).execute();
我已經能夠在Google雲端硬盤的基本目錄中創建一個新文件夾:
File body = new File();
body.setTitle("Air Note");
body.setMimeType("application/vnd.google-apps.folder");
File file = service.files().insert(body).execute();
我還能夠列出用戶的Google雲端硬盤帳戶中的所有文件夾:
List<File> files = service.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'").execute().getItems();
for (File f : files) {
System.out.println(f.getTitle() + ", " + f.getMimeType());
}
但是,我對如何將文本文件保存到Google雲端硬盤中的文件夾感到困惑。
您需要使用parent參數將文件放入使用insert的文件夾中。 有關詳細信息, 請訪問https://developers.google.com/drive/v2/reference/files/insert
像這樣的東西
File body = new File();
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
body.setParents(Arrays.asList(new File.ParentReference().setId(parentId));
File file = service.files().insert(body, textContent).execute();
如果要在Google雲端硬盤中的特定文件夾中插入文件,請按以下步驟操作。 讓我們假設我們已經從驅動器中檢索了所有文件夾,現在我將在列表的第一個文件夾中插入一個空文件,所以
//Getting Folders from the DRIVE
List<File> files = mService.files().list().setQ("mimeType = 'application/vnd.google-apps.folder'").execute().getItems();
File f =files.get(1)//getting first file from the folder list
body.setTitle("MyEmptyFile");
body.setMimeType("image/jpeg");
body.setParents(Arrays.asList(new ParentReference().setId(f.getId())));
com.google.api.services.drive.model.File file = mService.files().insert(body).execute();
現在,這將在文件夾中創建一個空文件,該文件位於檢索文件列表的頂部。
第1步:創建一個文件夾
File body1 = new File();
body1.setTitle("cloudbox");
body1.setMimeType("application/vnd.google-apps.folder");
File file1 = service.files().insert(body1).execute();
第2步:插入您的文件
File body2 = new File();
body2.setTitle(fileContent.getName());
body2.setMimeType("text/plain");
body2.setParents(Arrays.asList(new ParentReference().setId(file1.getId())));
File file2 = service.files().insert(body2, mediaContent).execute();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.