Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I've been trying to save a plain text file into a specific folder in Google Drive on Android.

So far using the Documentation and QuickStart Guide on Google Drive I have been able to do a few things that are going in the right direction, first I was able to create a plain text file:

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

I have been able to create a new folder in the base directory of Google Drive with:

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

I have also been able to list all of the folders in the user's Google Drive account with:

      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());
      }

However I am a bit stuck on how to save a text file into a folder within Google Drive.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
372 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to use the parent parameter to put a file in a folder using insert. More details at https://developers.google.com/drive/v2/reference/files/insert

Something like this

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...