将多个文件上传到Google云端硬盘 [英] Upload multiple files to Google Drive

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

问题描述

到目前为止,我一直在使用上传文件来驱动演示代码。但是,对于我的应用程序,我需要能够通过单击一个按钮来上传多个用户选择的文件。我的问题是,不是将每个文件上传到云端硬盘,它都会为每个选定的文件上传一次选择的最后一个文件。我认为,如果我正确地理解了这个问题的原因,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上传,0).show();
休息;
case REQUEST_CODE_CREATOR:
//将文件保存到Drive后调用。
if(resultCode == RESULT_OK){
Log.i(TAG,Files successfully saved。);
Toast.makeText(this,Starting new process,0).show();
file = null;
//返回主界面选择更多的应用程序等。
startActivityForResult(new Intent(this,MainActivity.class),
REQUEST_CODE_LAUNCH_MAIN);
}
break;

$ b $ / code $ / pre
$ b $ uploadToDrive() code> method

  public void uploadFileToDrive(){
//开始创建一个新内容,设置回调。
Log.i(标签,创建新内容);
Drive.DriveApi.newContents(googleApiClient).setResultCallback(新ResultCallback< DriveApi.ContentsResult>(){

@覆盖
公共无效onResult(DriveApi.ContentsResult结果){

if(!result.getStatus()。isSuccess()){
Log.i(TAG,创建新内容失败。);
return;


Log.i(TAG,创建新内容);
OutputStream outputStream = result.getContents()。getOutputStream();

尝试{
outputStream.write(file);
} catch(IOException e1){
Log.i(TAG,Unable to write file contents。);
}

//创建初始元数据 - MIME类型和标题
//注意用户可以稍后更改标题
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType(application / zip)
.setTitle(directory.getZipFileName())
.build();

//为文件选择器创建一个意图,并启动它。
IntentSender intentSender = Drive.DriveApi
.newCreateFileActivityBuilder()
.setInitialMetadata(metadataChangeSet)
.setInitialContents(result.getContents())
.build(googleApiClient);

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


解决方案

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


Up until now I've been using the upload a file to drive demo code. However, for my application I need to be able to upload multiple user selected files with one click of a button. My problem is that instead of upload each file to Drive it will upload the last file selected once for every file thats been selected. I think if I understand it correctly the cause of this has something to do the IntentSender being executed multiple times quickly and returning to the REQUEST_CODE_CREATOR case but I'm unable to see another way to structure the code.

@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() method

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

解决方案

I think the solution is to make file and directory parameters to the uploadFileToDrive() function. Right now your code is referencing the single global copy of those variables, and the callbacks are likely only firing after the last iteration in the for loop is complete.

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

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