使用GD API将大文件上传到Google云端硬盘 [英] Upload large files to Google Drive using GD API

查看:393
本文介绍了使用GD API将大文件上传到Google云端硬盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用适用于Android的新Drive API以编程方式将大文件上传到Google Drive。您能否给我建议一个带有最佳实践的教程?



我读过这篇 post on stack overflow ,但是他们在那里使用缓冲区加载整个内存。

解决方案

我一直在使用 Google快速入门作为起点。它显示了如何上传Bitmap,但很容易适应。他们还将整个事情保存在一个字节数组中,并将其写入Google Drive的输出流。如果你不想这样做,那么我会建议做这样的事情:

  / ** 
*创建一个新文件并将其保存到云端硬盘。
* /
private void saveFileToDrive(final File file){
//开始创建一个新内容并设置回调。
Log.i(标签,创建新内容);
Drive.DriveApi.newDriveContents(mGoogleApiClient)
.setResultCallback(新的ResultCallback< DriveApi.DriveContentsResult>(){

@Override
public void onResult(DriveApi.DriveContentsResult结果){
//如果操作失败,我们不能做任何事情
//并且必须
//失败
if(!result.getStatus()。isSuccess ()){
Log.i(TAG,无法创建新内容);
return;
}
//否则,我们可以将数据写入新内容
Log.i(TAG,New contents created。);
//获取内容的输出流
OutputStream outputStream = result.getDriveContents()。getOutputStream( ) ;
//从中写入位图数据。
尝试{
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。);
}
//创建初始元数据 - MIME类型和标题。
//请注意,用户稍后可以更改标题。
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setTitle(file.getName())。build();
//为文件选择器创建一个意图,并启动它。
IntentSender intentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialDriveContents(result.getDriveContents())
.build(mGoogleApiClient);
尝试{
startIntentSenderForResult(
intentSender,REQUEST_CODE_CREATOR,null,0,0,0);
} catch(IntentSender.SendIntentException e){
Log.i(TAG,Failed to launch file chooser。);
}
}
});
}

通过快速入门的设置,您可以使用此方法上传文件。

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.

解决方案

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.

这篇关于使用GD API将大文件上传到Google云端硬盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆