【问题标题】:Android resumable upload to Google DriveAndroid 可恢复上传到 Google Drive
【发布时间】:2014-06-27 15:24:20
【问题描述】:

我正在测试适用于 Android 的 Drive API 以上传一个文件,该文件可以显示上传进度并在失败时能够继续上传(文件大小 > 30 MB。)

有以下问题: Uploading Downloading of large size file to Google Drive giving error, Upload progress listener not fired (Google drive API) 我能够获得上传进度,他们提到那些是可恢复的上传。但是,我没有看到任何查找上传错误和恢复逻辑的代码,因此如果我终止应用程序并“恢复”上传,它只是从头开始。

这是我的代码:

public class DriveScreen extends BaseDriveActivity {
    private Drive service;
    private Context context;
    @Override
    public void onConnected(Bundle connectionHint) {
        super.onConnected(connectionHint);
        context = this;

        //https://stackoverflow.com/questions/17429798/usingoauth2-deprecated
        GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE_FILE));
        credential.setSelectedAccountName("accountNameHERE");
        service = new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();
        UploadFile();
    }

    public void UploadFile() {
        AsyncTask<Void, Long, String> task = new AsyncTask<Void, Long, String>() {
            java.io.File fileContent;
            FileContent mediaContent;
            com.google.api.services.drive.model.File body;
            com.google.api.services.drive.model.File file;
            private ProgressDialog mDialog;
            long mFileLen;

            @Override
            protected void onPreExecute() {
                // TODO Auto-generated method stub
                super.onPreExecute();
                mDialog = new ProgressDialog(context);
                mDialog.setMax(100);
                mDialog.setMessage("Uploading ");
                mDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                mDialog.setProgress(0);
                mDialog.setButton("Cancel", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub

                    }
                });
                mDialog.show();
            }

            class FileUploadProgressListener implements MediaHttpUploaderProgressListener {

                @Override
                public void progressChanged(MediaHttpUploader uploader) throws IOException {
                    Log.d("Percent ", String.valueOf(uploader.getProgress()));
                    switch (uploader.getUploadState()) {
                    case INITIATION_STARTED:
                        System.out.println("Initiation Started");
                        break;
                    case INITIATION_COMPLETE:
                        System.out.println("Initiation Completed");
                        break;
                    case MEDIA_IN_PROGRESS:
                        System.out.println("Upload in progress");
                        System.out.println("Upload percentage: " + uploader.getProgress());
                        mDialog.setProgress((int) (uploader.getProgress()*100));
                        break;
                    case MEDIA_COMPLETE:
                        System.out.println("Upload Completed!");
                        break;
                    case NOT_STARTED:
                        System.out.println("Upload Not Started!");
                        break;
                    }
                }
            }

            @Override
            protected String doInBackground(Void... arg0) {
                try {
                    File folder = Environment.getExternalStoragePublicDirectory(
                            Environment.DIRECTORY_PICTURES
                        );
                    File UPLOAD_FILE = new File(folder, "filePathHERE");
                    fileContent = new File(folder, "filePathHERE");
                    mFileLen = fileContent.length();

                    InputStreamContent mediaContent2 = new InputStreamContent("application/zip", new FileInputStream(UPLOAD_FILE));
                    mediaContent2.setLength(UPLOAD_FILE.length());
                    body = new com.google.api.services.drive.model.File();
                    body.setTitle(fileContent.getName());
                    body.setMimeType("application/zip");
                    //String parentId = null;
                    //int x = Files.List.setQ("mimeType = 'application/vnd.google-apps.folder' and title = 'ShareHim'");
                    //body.setParents(Arrays.asList(new ParentReference().setId(uploadFile.getFileHostFolderId())));



                    Files.Insert mInsert = service.files().insert(body, mediaContent2);
                    if(mFileLen > 5 * 1024 * 1024)
                    {
                        MediaHttpUploader uploader = mInsert.getMediaHttpUploader();
                        uploader.setDirectUploadEnabled(false);
                        uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE);
                        uploader.setProgressListener(new FileUploadProgressListener());
                        file = mInsert.execute();
                        if (file != null) {

                        }
                    }
                    else {
                        mInsert.execute();
                    }
                } catch (UserRecoverableAuthIOException e) {
                    System.out.println("login error");
                    Log.d("Error", "not login " + e);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }


            protected void onPostExecute(String result) {
                mDialog.dismiss();
            };
        };
        task.execute();
    }
}

现在,它是按块上传,但是我们如何才能从最后发送的块继续上传。在 youtube 上观看 Google Drive 演讲后,我的第一印象是可恢复上传是自动处理的,但似乎并非如此。

我是不是以错误的方式处理这个问题?是否值得考虑使用 DriveSyncAdapter 来上传文件作为替代方案? (抱歉代码不好,这只是一个测试)

【问题讨论】:

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


    【解决方案1】:

    看起来您正在使用 Java REST-ful API。如果您使用 Android 特定的 API,这一切都会为您处理。只需交出文件,API 将根据需要进行可恢复上传。见creating files.

    【讨论】:

    • 这就是我最初尝试的,但由于某种原因我无法让它恢复。你知道它是如何工作的,它怎么知道我什么时候尝试上传之前失败的同一个文件?另外,是否可以显示进度?
    • 文件怎么失败了?一旦你把它交给 API,任何必要的恢复都会在后台自动发生。除非您收到初始失败响应,否则您永远不需要重试。目前不支持上传进度。 (可以获取下载进度。)
    • 谢谢,你能帮我看看我是否理解正确吗?我再次从您的链接中实现了该方法。现在,我创建一个文件并在创建文件后收到通知。创建文件后,据我所知,如果有可用的连接,该文件将上传到云端硬盘。它似乎正在做我想做的事。有什么方法可以知道云端硬盘已成功接收到文件?感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 2013-07-28
    • 2016-06-25
    相关资源
    最近更新 更多