1

I want to upload large files to Google Drive programmatically using the new Drive API for android. Could you please suggest me a tutorial with best practices for this?

I was reading this post on stack overflow but there they load the whole in memory, using a buffer.

Community
  • 1
  • 1
Alexandru Circus
  • 5,478
  • 7
  • 52
  • 89
  • 1
    When loading content, you may use either a memory buffer (byte[]) or a file (java.io.File). It is a matter of your app's architecture. If your content exists as a byte[] buffer in your app, you write it into a stream, if it is a java.io.File, use the file. The stock camera app for instance produces a file, but custom camera handler may produce a byte[] buffer with jpeg content. – seanpj Dec 10 '15 at 14:10

1 Answers1

1

I've been using this quick start from Google as a starting point. It shows how to upload a Bitmap but it's easy to adapt. They also do save the whole thing in a byte array and write it to the output stream for Google Drive. If you don't want to do this then I would recommend doing something like this instead:

     /**
     * Create a new file and save it to Drive.
     */
    private void saveFileToDrive(final File file) {
        // Start by creating a new contents, and setting a callback.
        Log.i(TAG, "Creating new contents.");
        Drive.DriveApi.newDriveContents(mGoogleApiClient)
                .setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {

                    @Override
                    public void onResult(DriveApi.DriveContentsResult result) {
                        // If the operation was not successful, we cannot do anything
                        // and must
                        // fail.
                        if (!result.getStatus().isSuccess()) {
                            Log.i(TAG, "Failed to create new contents.");
                            return;
                        }
                        // Otherwise, we can write our data to the new contents.
                        Log.i(TAG, "New contents created.");
                        // Get an output stream for the contents.
                        OutputStream outputStream = result.getDriveContents().getOutputStream();
                        // Write the bitmap data from it.
                        try {
                            FileInputStream fileInputStream = new FileInputStream(file);
                            byte[] buffer = new byte[1024];
                            int bytesRead;
                            while ((bytesRead = fileInputStream.read(buffer)) != -1)
                            {
                                outputStream.write(buffer, 0, bytesRead);
                            }
                        } catch (IOException e1) {
                            Log.i(TAG, "Unable to write file contents.");
                        }
                        // Create the initial metadata - MIME type and title.
                        // Note that the user will be able to change the title later.
                        MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                                .setTitle(file.getName()).build();
                        // Create an intent for the file chooser, and start it.
                        IntentSender intentSender = Drive.DriveApi
                                .newCreateFileActivityBuilder()
                                .setInitialMetadata(metadataChangeSet)
                                .setInitialDriveContents(result.getDriveContents())
                                .build(mGoogleApiClient);
                        try {
                            startIntentSenderForResult(
                                    intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
                        } catch (IntentSender.SendIntentException e) {
                            Log.i(TAG, "Failed to launch file chooser.");
                        }
                    }
                });
    }

With the setup from the quickstart you could use this method to upload your file.

Emanuel Seidinger
  • 1,272
  • 1
  • 12
  • 21
  • Thanks, I read that the driveContents it's actually a duplicate of the original file, so I think when you write to it's buffer that buffer is flushed into that copy file so it's not hold in memory. I will upload audio recordings that can be very large. – Alexandru Circus Dec 10 '15 at 10:45