How to upload recorded videos to Google Drive using Android Studio

I’m working on an Android app that needs to upload video files to Google Drive. I can already record videos using camera intents and save them to device storage, but I’m stuck on the upload part.

I found Google’s quickstart example for uploading images, but I need to modify it for video files instead. The main difference should be handling video data rather than bitmap data, but I’m not sure about the exact implementation.

Here’s a modified version of what I’m trying to achieve:

/**
 * Video upload activity for Google Drive integration
 */
public class VideoUploadActivity extends Activity implements ConnectionCallbacks,
        OnConnectionFailedListener {

    private static final String LOG_TAG = "video-drive-upload";
    private static final int VIDEO_CAPTURE_REQUEST = 100;
    private static final int FILE_CREATOR_REQUEST = 200;
    private static final int CONNECTION_RESOLUTION_REQUEST = 300;

    private GoogleApiClient driveApiClient;
    private Uri videoFileUri;

    private void uploadVideoToDrive() {
        Log.d(LOG_TAG, "Starting video upload process.");
        final Uri videoUri = videoFileUri;
        Drive.DriveApi.newDriveContents(driveApiClient)
                .setResultCallback(new ResultCallback<DriveContentsResult>() {

                    @Override
                    public void onResult(DriveContentsResult uploadResult) {
                        if (!uploadResult.getStatus().isSuccess()) {
                            Log.e(LOG_TAG, "Could not create drive contents.");
                            return;
                        }
                        
                        Log.d(LOG_TAG, "Drive contents ready for upload.");
                        OutputStream driveOutputStream = uploadResult.getDriveContents().getOutputStream();
                        
                        try {
                            InputStream videoInputStream = getContentResolver().openInputStream(videoUri);
                            byte[] buffer = new byte[1024];
                            int bytesRead;
                            while ((bytesRead = videoInputStream.read(buffer)) != -1) {
                                driveOutputStream.write(buffer, 0, bytesRead);
                            }
                            videoInputStream.close();
                        } catch (IOException ex) {
                            Log.e(LOG_TAG, "Error writing video data.");
                        }
                        
                        MetadataChangeSet videoMetadata = new MetadataChangeSet.Builder()
                                .setMimeType("video/mp4")
                                .setTitle("RecordedVideo.mp4")
                                .build();
                        
                        IntentSender fileChooserIntent = Drive.DriveApi
                                .newCreateFileActivityBuilder()
                                .setInitialMetadata(videoMetadata)
                                .setInitialDriveContents(uploadResult.getDriveContents())
                                .build(driveApiClient);
                        
                        try {
                            startIntentSenderForResult(
                                    fileChooserIntent, FILE_CREATOR_REQUEST, null, 0, 0, 0);
                        } catch (SendIntentException ex) {
                            Log.e(LOG_TAG, "Could not start file chooser.");
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent resultData) {
        switch (requestCode) {
            case VIDEO_CAPTURE_REQUEST:
                if (resultCode == Activity.RESULT_OK) {
                    videoFileUri = resultData.getData();
                    uploadVideoToDrive();
                }
                break;
            case FILE_CREATOR_REQUEST:
                if (resultCode == RESULT_OK) {
                    Log.d(LOG_TAG, "Video upload completed successfully.");
                    videoFileUri = null;
                }
                break;
        }
    }
}

Is this the right approach for handling video uploads? Any suggestions on how to properly stream video data to Google Drive would be really helpful!

Your Google Drive API is dead - Google killed the Drive Android API in December 2021. You need to switch to the REST API with Google Sign-In. I did this migration last year and it’s actually easier than the old API. Use Drive REST API v3 with GoogleSignInAccount for auth. File uploads become simple HTTP requests through the Drive service object. For videos, handle the async upload properly since these files can be hundreds of MB. Add exponential backoff for retries - mobile connections suck during long uploads. If file size matters, compress videos first. MediaMetadataRetriever can grab video dimensions and duration to help you decide if compression’s worth it. The REST API handles large uploads way better than the old Android API anyway.

Your code looks solid overall, but I hit a few issues when I tried something similar. The biggest problem is you’re not closing the output stream after writing the video data - this causes upload failures or corrupted files. You need to call driveOutputStream.close() in a finally block after writing.

Another thing - video files are huge, and your approach loads everything into memory at once. For larger videos, consider chunked uploading or at least add a progress bar for better UX.

I also noticed you’re hardcoding the MIME type as “video/mp4” but depending on camera settings, you might get different formats. Use ContentResolver to detect the actual MIME type from the URI.

One more tip from my experience - validate that the video file exists and is readable before uploading. Camera intents can be weird across different device manufacturers.

the biggest pain i had with video uploads was memory issues. your 1024-byte buffer is way too small for video files - bump it to at least 8192 or 16384. also, handle screen rotation during uploads. i lost tons of uploads because users rotated their phone mid-upload and killed the activity.