2013-01-20 159 views
4

我一直在尝试将纯文本文件保存到Android上Google云端硬盘中的特定文件夹中。将文件保存在Google Drive SDK的特定文件夹中

到目前为止使用我已经能够做到在正确的方向去的几件事情在谷歌云端硬盘中的文件和QuickStart Guide,首先,我能创造一个纯文本文件:

File body = new File(); 
    body.setTitle(fileContent.getName()); 
    body.setMimeType("text/plain"); 
    File file = service.files().insert(body, textContent).execute(); 

我已经能够在谷歌Drive大道的根目录创建一个新的文件夹:

File body = new File(); 
    body.setTitle("Air Note"); 
    body.setMimeType("application/vnd.google-apps.folder"); 
    File file = service.files().insert(body).execute(); 

我也一直能与列出的所有文件夹在用户的谷歌云端硬盘帐户:

 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云端硬盘中的文件夹。

回答

7

您需要使用父参数将文件放入使用插入的文件夹中。在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(); 
+0

工作就像一个魅力,非常感谢你! =) – Gatekeeper

+0

你如何获得“parentId”的保留? –

+0

如果你知道你要去哪里商店,它几乎直截了当 – the100rabh

1

如果要插入的特定文件夹的文件,在谷歌驱动器,然后按照这些步骤。让我们假设,我们已经检索到的所有文件夹从驱动器,现在我将输入在列表中的第一个文件夹中的空文件,所以

  //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(); 
相关问题