6

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

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

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.

How to make It work?

Here is my Upload code:

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();
}
Community
  • 1
  • 1
Tai Dao
  • 3,407
  • 7
  • 31
  • 54
  • 1
    I did not test the code so I am replying this as a comment instead of answer 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. – tcboy88 Aug 01 '13 at 18:14
  • @tcboy88 that's right, just add setDirectUploadEnabled and setChunkSize then it worked !! – Tai Dao Aug 02 '13 at 01:02
  • @Dolphin .. can you please provide full code , as i am getting errors in import and some of the dependencies are not fully resolved. Please help. – SimpleCoder Jun 04 '17 at 08:02
  • @SimpleCoder This post is too old and I don't keep the code any more .. – Tai Dao Jun 04 '17 at 13:10
  • @Dolphin.. ok No problem . I ll keep searching til i find. :P – SimpleCoder Jun 06 '17 at 15:10

1 Answers1

10

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.

IvanRF
  • 7,115
  • 5
  • 47
  • 71
tcboy88
  • 1,063
  • 1
  • 9
  • 23