其他分享
首页 > 其他分享> > android-使用GD API将大文件上传到Google云端硬盘

android-使用GD API将大文件上传到Google云端硬盘

作者:互联网

我想使用新的Android云端硬盘API以编程方式将大文件上传到Google云端硬盘.您能为我推荐一个具有最佳实践的教程吗?

我正在读取此post on stack overflow,但在那里使用缓冲区将它们加载到内存中.

解决方法:

我一直以quick start from Google为起点.它显示了如何上传位图,但很容易适应.他们还会将整个内容保存为字节数组,然后将其写入Google云端硬盘的输出流.如果您不想执行此操作,那么我建议您执行以下操作:

     /**
     * 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.");
                        }
                    }
                });
    }

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

标签:android,google-drive-api
来源: https://codeday.me/bug/20191013/1909096.html