0

Good time of the day.
I'm trying to make application which is supposed to upload files to server and display progress of uploading in ProgressDialog.

This is the code i have so far:
Create ProgressDialog for Uploading Procedure

private void createUploadingDialog(int filesCount) {
    uploadingDialog = new ProgressDialog(getActivity());
    uploadingDialog.setIndeterminate(true);
    loadingDialog.setMessage(getString(R.string.upload_progress, 0, filesCount));
    loadingDialog.show();
}

Dismiss ProgressDialog for Uploading Procedure

private void dismissUploadingDialog() {
    if (uploadingDialog.isShowing())
        uploadingDialog.dismiss();
}

Update ProgressDialog for Uploading Procedure

private void updateUploadingDialog(final String dialogMessage) {
    class UpdateDialog implements Runnable {
        private String messageString;
        private UpdateDialog(String ms) { messageString = ms; }
        public void run() {
            uploadingDialog.setMessage(messageString);
        }
    }
    getActivity().runOnUiThread(new UpdateDialog(dialogMessage));
}

Call to create dialog

createUploadingDialog(imagesPathsArray.size());

Call to dismiss dialog

dismissUploadingDialog();

Call to update dialog onSuccess

updateUploadingDialog(getString(R.string.upload_progress, i, imagesPathsArray.size()));

Call to update dialog onFailure

updateUploadingDialog(getString(R.string.retrying_request));

The dialog is displayed in fragment. To send images to server i'm using Retrofit2.

When i do initial setup of dialog, message get set correctly (showing total files and current one), however, message is not updating when the update call is made. For some reason, no error is thrown and if i'm adding Log.d, it even shows that method was called.

Thank you for any help!

Ivan Zhivolupov
  • 1,107
  • 2
  • 20
  • 39
  • you should read this .This will you help. http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog?rq=1 – Ramchandra Singh Nov 29 '16 at 03:14

2 Answers2

0

I think you are calling update progress only once , how are you listning to progress state change listener?, if you want to use that you have to continuously call updateUploadingDialog which is not a good procedure

You have to write upload procedure in asynctask and update progress in onProgressUpdate() method

Here and here are example for reference

on preExecute initialize the progress bar, in doinbackground upload the file, in onProgressUpdate update the progress bar and on postexecute do what ever you want to do after upload

Hope this helps

Community
  • 1
  • 1
Manohar
  • 22,116
  • 9
  • 108
  • 144
0

As per you are not using AsyncTask, you can't get the opportunity of onProgressUpdate feature. So, you have to update your progress bar status manually like using own thread and you have already do. But, I solved my problem using below source code, you can check it:

  private void createThread(){
    Runnable runable = new updateProgressStatus();
    progressUpdateThread = new Thread(runnable);
    progressUpdateThread.start();
}

public class updateProgressStatus implements Runnable {
    public void run() {
        while(Thread.currentThread()==progressUpdateThread)            
            try {
                Thread.sleep(1000);    
                progressHandler.sendMessage(YOUR MESSAGE );
            } catch (InterruptedException e) {
                // Interrupt problem
                Thread.currentThread().interrupt();
            } catch (Exception e) {
        e.printStackTrace();
        }
    }
}

private Handler progressHandler = new Handler(){
    @Override
    public void handleMessage(Message message) {
        // Now, update your progress bar status
        progress.setProgress(message.what);
    }
};
Ratna Halder
  • 1,954
  • 16
  • 17