将多个文件上传到Google云端硬盘

时间:2014-05-26 23:57:00

标签: android google-drive-api google-drive-android-api

到目前为止,我一直在使用上传文件来驱动演示代码。但是,对于我的应用程序,我需要只需单击一个按钮就可以上传多个用户选择的文件。我的问题是,不是将每个文件上传到云端硬盘,而是上传为每个已选择的文件选择一次的最后一个文件。我想如果我理解正确的原因,那么IntentSender会快速执行多次并返回到REQUEST_CODE_CREATOR的情况,但是我无法看到构建代码的另一种方法。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CODE_LAUNCH_MAIN:
            if (resultCode == Activity.RESULT_OK) {
                Bundle bundle = data.getExtras();
                for (int i = 0; i<bundle.size()/2; i++) {
                    file = bundle.getByteArray(DATA+i);
                    directory = new Directory(bundle.getString(PATH+i));
                    Log.i(TAG, bundle.getString(PATH+i) + " extracted");
                    uploadFileToDrive();
                    Log.i(TAG, bundle.getString(PATH+i) + " uploaded");
                }
            }
            Toast.makeText(this, "Finished Uploading", 0).show();
            break;
        case REQUEST_CODE_CREATOR:
            // Called after a file is saved to Drive.
            if (resultCode == RESULT_OK) {
                Log.i(TAG, "Files successfully saved.");
                Toast.makeText(this, "Starting new process", 0).show();
                file = null;
                // Return to the Main UI to select more apps ect.
                startActivityForResult(new Intent(this, MainActivity.class),
                        REQUEST_CODE_LAUNCH_MAIN);
            }
            break;
    }
}

uploadToDrive()方法

public void uploadFileToDrive() {
    // Start by creating a new contents, and setting a callback.
    Log.i(TAG, "Creating new contents.");
    Drive.DriveApi.newContents(googleApiClient).setResultCallback(new ResultCallback<DriveApi.ContentsResult>() {

        @Override
        public void onResult(DriveApi.ContentsResult result) {

            if (!result.getStatus().isSuccess()) {
                Log.i(TAG, "Failed to create new contents.");
                return;
            }

            Log.i(TAG, "New contents created.");
            OutputStream outputStream = result.getContents().getOutputStream();

            try {
                outputStream.write(file);
            } 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()
                    .setMimeType("application/zip")
                    .setTitle(directory.getZipFileName())
                    .build();

            // Create an intent for the file chooser, and start it.
            IntentSender intentSender = Drive.DriveApi
                    .newCreateFileActivityBuilder()
                    .setInitialMetadata(metadataChangeSet)
                    .setInitialContents(result.getContents())
                    .build(googleApiClient);

            try {
                startIntentSenderForResult(
                        intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
            } catch (IntentSender.SendIntentException e) {
                Log.i(TAG, "Failed to launch file chooser.");
            }
        }
    });
}

1 个答案:

答案 0 :(得分:3)

我认为解决方案是将filedirectory参数设为uploadFileToDrive()函数。现在你的代码引用了这些变量的单个全局副本,并且回调很可能只在for循环的最后一次迭代完成后触发。