Search code examples
androidaudiogoogle-drive-android-api

How to Upload Multiple Audio Files to Google Drive To a Specified Folder in Drive?


I am working in a project that have to upload multiple audio files (.amr) to google drive to a specific folder. Now I am going through "startIntentSender" method to show a picker to choose a folder in drive. For a single file it is working fine, But I need to send multiple files at a time without any picker and I have to send that to a specific folder that I need to specify in program. I have searched a lot but I am getting confused. Now Google Drive Api is deprecated. Is there any method for multiple uploading ?

Drive.DriveApi.newDriveContents(mGoogleApiClient)
                .setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {

                    @Override
                    public void onResult(DriveApi.DriveContentsResult result) {

                        if (!result.getStatus().isSuccess()) {
                            Log.i(TAG, "Failed to create new contents.");
                            return;
                        }
                        OutputStream   outputStream = result.getDriveContents().getOutputStream();


                            try {
                                FileInputStream fileInputStream = new FileInputStream(mAllCallRecordFiles.get(i).getFilePath());
                                byte[] buffer = new byte[1024];
                                int bytesRead;
                                while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                                    outputStream.write(buffer, 0, bytesRead);
                                }
                            } catch (IOException e1) {
                                Log.i(TAG, "U AR A MORON! Unable to write file contents.");
                            }




                        MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                                .setMimeType("audio/*")
                                .build();

                        IntentSender intentSender = Drive.DriveApi
                                .newCreateFileActivityBuilder()
                                .setInitialMetadata(metadataChangeSet)
                                .setInitialDriveContents(result.getDriveContents())
                                .build(mGoogleApiClient);
                        try {
                            startIntentSender(intentSender, null, 0, 0, 0);
                        } catch (IntentSender.SendIntentException e) {
                            Log.i(TAG, "Failed to launch file chooser.");
                        }
                    }
                })

Solution

  • I suggest you yo use the REST part of google drive's api to do that: https://developers.google.com/drive/api/v2/about-sdk

    It looks like there is no way to send more than one file in a single request, but you can just make a loop and send multiple requests each one with a single file, then you will be uploading them at the same time anyway. This can help you with that: https://developers.google.com/drive/api/v2/manage-uploads

    This way you can choose the folder you want setting the proper id in the parent property of the file, as stated in the api documentation:

    String folderId = "0BwwA4oUTeiV1TGRPeTVjaWRDY1E";
    File fileMetadata = new File();
    fileMetadata.setTitle("photo.jpg");
    fileMetadata.setParents(Collections.singletonList(
        new ParentReference().setId(folderId)));
    java.io.File filePath = new java.io.File("files/photo.jpg");
    FileContent mediaContent = new FileContent("image/jpeg", filePath);
    File file = driveService.files().insert(fileMetadata, mediaContent)
        .setFields("id, parents")
        .execute();
    System.out.println("File ID: " + file.getId());
    

    This is the full documentation on how to manage folders https://developers.google.com/drive/api/v2/folder

    I hope this helps you.