Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save file in specific folder with Google Drive SDK

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.

like image 687
Gatekeeper Avatar asked Jan 20 '13 07:01

Gatekeeper


People also ask

How do I change where Google Drive saves?

To change the location of the Google Drive folder, click on the “Change…” link. This will open another popup window where you can choose the new location for your Google Drive folder. Once you've selected the folder, click “Start”. The program will now initiate the backup and sync process.


2 Answers

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();
like image 186
the100rabh Avatar answered Oct 07 '22 04:10

the100rabh


If you want to INSERT a file in specific folder in Google Drive then follow these steps. Lets assume that we have retrieved all folder from the Drive and now i will INSERT an empty file in the first folder from the list, So

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

Now this will create an empty file in the Folder which are at the top in the retrieve file list.

like image 1
Pir Fahim Shah Avatar answered Oct 07 '22 05:10

Pir Fahim Shah