0

I'm learning about Google Api for uploading file on Android and did find the good sample about it.(you can take a look on that sample here: https://github.com/sdivakarrajesh/Uploading-Files-to-Google-drive-using-java-client-api-in-Android/blob/master/app/src/main/java/com/dev/theblueorb/usingdrivejavaapi/DriveActivity.java) However, it's only show how to upload files, not how to select the file and upload it to GG drive. Here are the code for uploading and creating folder on GG drive:

private void uploadFile() throws IOException {
        File fileMetadata = new File();;
        fileMetadata.setName("Sample File");
        fileMetadata.setMimeType("application/vnd.google-apps.spreadsheet");

        // For mime type of specific file visit Drive Doucumentation

        file2 = new java.io.File(path);
        InputStream inputStream = getResources().openRawResource(R.raw.template);
        try {
            FileUtils.copyInputStreamToFile(inputStream,file2);
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileContent mediaContent = new FileContent("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",file2);


        File file = mService.files().create(fileMetadata, mediaContent)
                .setFields("id")
                .execute();


        Log.e(this.toString(),"File Created with ID:"+ file.getId());

        Toast.makeText(getApplicationContext(),
                "File created:"+file.getId() , Toast.LENGTH_SHORT).show();
    }


}




private void createFolderInDrive() throws IOException {
        File fileMetadata = new File();
        fileMetadata.setName("Sample Folder");
        fileMetadata.setMimeType("application/vnd.google-apps.folder");

        File file = mService.files().create(fileMetadata)
                .setFields("id")
                .execute();
        System.out.println("Folder ID: " + file.getId());

        Log.e(this.toString(),"Folder Created with ID:"+ file.getId());
        Toast.makeText(getApplicationContext(),
                "Folder created:"+file.getId() , Toast.LENGTH_SHORT).show();
    }

Any body knows how to select the file on device, then upload it to selected folder on GG drive or the sample for that?

Kranatos
  • 107
  • 2
  • 10

1 Answers1

0

Referencing these docs for basic file upload, you should be able to "select the file on device" by specifying the complete file path as fileName in this line below:

java.io.File fileContent = new java.io.File(filename);.

For example, if you had a file called coolpic inside the directory media, you could use media/coolpic for the filename. The next doc I reference reinforces this strategy. The path will depend on the root location which, is something you can easily investigate.

Then, check out this doc for working with folders in Google Drive. You'll want to find the folder id and set this on upload using

fileMetadata.setParents(Collections.singletonList(folderId));

Note you can upload and then move in two steps, or use my method above and set the folder on upload.

Matt Goodrich
  • 4,875
  • 5
  • 25
  • 38
  • Thank you for your reply. But about the "selecting file", I meant something like a cursor, so I browse through directory tree to get the file. Is the any good sample for that? Thanks ! – Kranatos Jan 09 '18 at 15:42
  • @Matt Goodrich Thanks for the response, you're getting me closer to what I need. But how do I create a Picker in an Android app that lets the user choose which folder to save the file to using the REST API? Ideally, that picker would return the folder ID that the user selects, and then I would use that to save the file. Thanks in advance! – shagberg Dec 18 '18 at 15:45