以编程方式将视频上传到Google云端硬盘(Android API) [英] Uploading video to Google Drive programmatically (Android API)
问题描述
我遵循了Drive API指南( https://developer.android .com/google/play-services/drive.html ),我的应用现在可以顺利上传照片了,但是我现在尝试上传视频(mp4)却没有成功.
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.
有人知道如何实现这一目标吗?该视频是一个新生成的mp4文件,我具有将其存储在设备上的路径.
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());
}
});
}
我感兴趣的是File的替代方案,在这种情况下,它指向"video/mp4".
What I am interested in is an alternative for a File, in this case pointing to a "video/mp4".
推荐答案
在此不加赘述,仅提供一些指针:
Without getting into much detail, just a few pointers:
您要上传的任何内容(图片,文字,视频等)都来自
Anything you want to upload (image, text, video,...) consists from
- 创建文件
- 设置元数据(标题,MIME类型,描述等)
- 设置内容(字节流)
您提到的演示使用图像(JPEG字节流)进行处理,而您需要对视频进行处理.因此,您需要实施的更改是:
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:
- 将"image/jpeg" MIME类型替换为您所需的一种 视频
- 复制视频流( outputStream.write(bitmapStream.toByteArray())... )
- replace the "image/jpeg" MIME type with the one you need for your video
- copy your video stream (outputStream.write(bitmapStream.toByteArray())...)
到内容.
这些是您唯一需要进行的更改. Google Drive Android API不在乎您的内容和元数据,只是抓住了它就把它推到了Google Drive中.
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.
在Google云端硬盘中,应用程序(网络,Android等)读取元数据和内容,并进行相应处理.
In Google Drive, apps (web, android,...) read the metadata and content, and treat it accordingly.
这篇关于以编程方式将视频上传到Google云端硬盘(Android API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!