androidgoogle-drive-android-api

Uploading video to Google Drive programmatically (Android API)


I have followed the Drive API guide (https://developer.android.com/google/play-services/drive.html) and my app now uploads photos smoothly, but I am now trying to upload videos (mp4) without success.

Does anyone know how to achieve this? The video is a newly generated mp4 file and I have the path to where it is stored on the device.

For pictures its done like this:

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

@Override
public void onResult(DriveContentsResult result) {
    if (!result.getStatus().isSuccess()) {
        Log.i(TAG, "Failed to create new contents.");
        return;
    }
    OutputStream outputStream = result.getDriveContents().getOutputStream();
    // Write the bitmap data from it.
    ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 80, bitmapStream);
    try {
        outputStream.write(bitmapStream.toByteArray());
    } catch (IOException e1) {
        Log.i(TAG, "Unable to write file contents.");
    }
    image.recycle();
    outputStream = null;
    String title = Shared.getOutputMediaFile(Shared.MEDIA_TYPE_IMAGE).getName();
    MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
        .setMimeType("image/jpeg").setTitle(title)
        .build();
    Log.i(TAG, "Creating new pic on Drive (" + title + ")");

    Drive.DriveApi.getFolder(mDriveClient,
        mPicFolderDriveId).createFile(mDriveClient,
        metadataChangeSet, result.getDriveContents());
        }
    });
}

What I am interested in is an alternative for a File, in this case pointing to a "video/mp4".


Solution

  • Without getting into much detail, just a few pointers:

    Anything you want to upload (image, text, video,...) consists from

    1. creating a file
    2. setting metadata (title, MIME type, description,...)
    3. setting content (byte stream)

    The demo you mention does it with an image (JPEG bytestream) and you need to do it with video. So, the changes you need to implement are:

    to the content.

    These are the only changes you need to make. Google Drive Android API doesn't care what is your content and metadata, it just grabs it a shoves it up to Google Drive.

    In Google Drive, apps (web, android,...) read the metadata and content, and treat it accordingly.