使用Google Drive SDK将文件保存在特定文件夹中

Gat*_*per 5 java android google-drive-api

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

到目前为止,使用Google云端硬盘上的文档和快速入门指南,我已经能够做一些正确的方向,首先我能够创建纯文本文件:

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

我已经能够在Google云端硬盘的基本目录中创建一个新文件夹:

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

我还能够列出用户的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());
      }
Run Code Online (Sandbox Code Playgroud)

但是,我对如何将文本文件保存到Google云端硬盘中的文件夹感到困惑.

the*_*abh 6

您需要使用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();
Run Code Online (Sandbox Code Playgroud)