上传进度侦听器未触发(Google 驱动器 API)

Upload progress listener not fired (Google drive API)(上传进度侦听器未触发(Google 驱动器 API))
本文介绍了上传进度侦听器未触发(Google 驱动器 API)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想通过使用 Google Drive API 上传文件的百分比来显示进度.

I want to show progress by percent of an uploading file using Google drive API.

我的menthod之前已经成功上传了一个文件,但是看不到上传进度.

My menthod has successful upload a file before but It can't see upload progress.

我已经看到并添加了 this FileUploadProgressListener 但 mediaHttpUploader.getProgress() 仅显示 0.0(进度开始)和 1.0(进度完成时).我无法及时获得进度的百分比.

I've seen and added this FileUploadProgressListener but mediaHttpUploader.getProgress() show only 0.0 (start of progress) and 1.0 (when progress finished). I can't get percent of progress in time.

如何使它工作?

这是我的上传代码:

public void UploadFile(final DFile uploadFile) {
    if (!isLoggedIn()) {
        OnUploadGoogleChecked(FALSE, "Not logged in");
        return;
    }
    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(act);
            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("Dolphin got 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());
                    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 {

                java.io.File UPLOAD_FILE = new java.io.File(uploadFile.getNameAndDir());
                // File's metadata.
                fileContent = new java.io.File(uploadFile.getNameAndDir());
                mFileLen = fileContent.length();

                InputStreamContent mediaContent2 = new InputStreamContent("image/jpeg", new BufferedInputStream(new FileInputStream(UPLOAD_FILE)));
                mediaContent2.setLength(UPLOAD_FILE.length());
                body = new com.google.api.services.drive.model.File();
                body.setTitle(fileContent.getName());
                body.setMimeType("image/jpeg");
                String parentId = null;
                body.setParents(Arrays.asList(new ParentReference().setId(uploadFile.getFileHostFolderId())));


                Drive.Files.Insert mInsert = service.files().insert(body, mediaContent2);
                MediaHttpUploader uploader = mInsert.getMediaHttpUploader();
                uploader.setDirectUploadEnabled(true);
                uploader.setProgressListener(new FileUploadProgressListener());
                file = mInsert.execute();
                if (file != null) {

                }

            } catch (UserRecoverableAuthIOException e) {

                Log.d("Dolphin got error", "not login " + e);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }


        protected void onPostExecute(String result) {
            mDialog.dismiss();
            OnUploadGoogleChecked(TRUE, "Upload complete");
        };
    };
    task.execute();
}

推荐答案

要获得进度,必须使用 uploader.setDirectUploadEnabled(false);(而不是 true)和 uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE); 或任何必须是 MediaHttpUploader.MINIMUM_CHUNK_SIZE 的乘积的值,否则会出错.

To get progress, you must use uploader.setDirectUploadEnabled(false); (instead of true) and uploader.setChunkSize(MediaHttpUploader.MINIMUM_CHUNK_SIZE); or any value which must be the multiply of MediaHttpUploader.MINIMUM_CHUNK_SIZE, else it will cause error.

这篇关于上传进度侦听器未触发(Google 驱动器 API)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)