使用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参数将文件插入到文件夹中。有关更多详细信息,访问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();