Skip to content Skip to sidebar Skip to footer

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 t

Solution 1:

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

Filebody=newFile();  
body.setTitle(fileContent.getName());
body.setMimeType("text/plain");
body.setParents(Arrays.asList(newFile.ParentReference().setId(parentId));  
Filefile= service.files().insert(body, textContent).execute();

Solution 2:

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

   Filef=files.get(1)//getting first file from the folder list
    body.setTitle("MyEmptyFile");
    body.setMimeType("image/jpeg");
    body.setParents(Arrays.asList(newParentReference().setId(f.getId())));
    com.google.api.services.drive.model.Filefile= 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.

Solution 3:

Step1 : Create a folder

Filebody1=newFile();
body1.setTitle("cloudbox");
body1.setMimeType("application/vnd.google-apps.folder");
Filefile1= service.files().insert(body1).execute();

Step2 : Insert your file

Filebody2=newFile();  
body2.setTitle(fileContent.getName());
body2.setMimeType("text/plain");
body2.setParents(Arrays.asList(newParentReference().setId(file1.getId())));  
Filefile2= service.files().insert(body2, mediaContent).execute();

Post a Comment for "Save File In Specific Folder With Google Drive Sdk"