0

I have this code but the progress bar does not update the uploaded bytes/lenght of the file.

The progress dialog is displayed correctly but the progress stays in 0, then it simply disappears, the file was uploaded correctly but no progress is updated.

private class UploadFile extends AsyncTask<String, Integer, Boolean> {
    @Override
    protected Boolean doInBackground(String... params) {
        FTPClient client = null;
        String filePath = params[0];
        try {
            // Get the FTP Connection from the Utility class
            client = FTPUtility.connect(ipAddress, userName, password);
            //if directory is not there, create it.
            try {
                client.changeDirectory(params[1]);
            } catch(Exception e) {
                client.createDirectory(params[1]);
                client.changeDirectory(params[1]);
            }

            if (client != null) {
                try {
                    // Define the File with complete path to be uploaded
                    File fileUpload = new File(filePath);

                    fileSize= fileUpload.length();
                    Log.d("FTPSync", "File Size: "+fileSize);
                    Log.d("FTPSync", "Uploading the " + filePath
                            + " to Remote Machine");

                    // Upload the file
                    client.upload(fileUpload, new FTPDataTransferListener() {
                        @Override
                        public void started() {
                            // Transfer started
                            Log.d("FTP","TRANSFER-STATUS: File transfer started...");
                        }
                        @Override
                        public void transferred(int length) {
                            int progress = (length*100)/((int)fileSize);
                            publishProgress(progress);
                            Log.d("FTP","Progress: "+progress);
                        }
                        @Override
                        public void completed() {           
                            Log.d("FTP","TRANSFER-STATUS: File transfer completed...");
                        }
                        @Override
                        public void aborted() {
                            // Transfer aborted
                            Log.d("FTP","TRANSFER-STATUS: File transfer aborted...");
                        }
                        @Override
                        public void failed() {
                            // Transfer failed
                            Log.d("FTP","TRANSFER-STATUS: File transfer failed...");

                        }
                    });
                    Log.d("FTPSync", "Successfully Uploaded the "
                            + filePath + " File to Remote Machine");

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        finally {
            if (client != null) {
                try {
                    client.disconnect(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        pDialog.dismiss();
        Toast.makeText(context, "Operation Completed", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPreExecute() {
        pDialog = new ProgressDialog(context);
        pDialog.setMessage(message);
        pDialog.setIndeterminate(true);
        pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        pDialog.setProgress(values[0]);
    }
}
private static class FTPUtility {
    public static FTPClient connect(String ipAddress, String userName,
            String password) {
        FTPClient client = new FTPClient();
        Log.d("FTP","Connecting to " + ipAddress);
        try {
            client.setType(FTPClient.TYPE_BINARY);
            client.connect(ipAddress);
            client.login(userName, password);
            return client;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

The debug shows only one entry about Progress:100

How can I make the progress bar update.

NOTE: I've tried with small and large files, so it seems to be the same issue in both file sizes.

Alex Lord Mordor
  • 2,890
  • 7
  • 27
  • 47

3 Answers3

0

All you need is to sum length and put it in ProgressBar. Something like this:

public class MyTransferListener implements FTPDataTransferListener {

    JProgressBar jp;
    int transfBytes=0;

    public MyTransferListener(JProgressBar jp){
        this.jp=jp;
    }

public void started() {
    // Transfer started
        jp.setValue(0);
}

public void transferred(int length) {
    // Yet other length bytes has been transferred since the last time this
    // method was called
        transfBytes+=length;
        jp.setValue(transfBytes);
}

public void completed() {
    // Transfer completed
        jp.setValue(jp.getMaximum());

}

public void aborted() {
    // Transfer aborted
}

public void failed() {
    // Transfer failed
} 

}

0
client.upload(fileUpload, new FTPDataTransferListener() {
                    int progress = 0;
                    @Override
                    public void started() {
                        // Transfer started
                        Log.d("FTP","TRANSFER-STATUS: File transfer 
started...");
                    }
                    @Override
                    public void transferred(int length) {
                        progress = progress + length;
                        publishProgress((int) (((float)progress * 
(float)100) / (float)fileSize));
                        Log.d("FTP","Progress: "+progress);
                    }
                    @Override
                    public void completed() {           
                        Log.d("FTP","TRANSFER-STATUS: File transfer 
completed...");
                    }
                    @Override
                    public void aborted() {
                        // Transfer aborted
                        Log.d("FTP","TRANSFER-STATUS: File transfer 
aborted...");
                    }
                    @Override
                    public void failed() {
                        // Transfer failed
                        Log.d("FTP","TRANSFER-STATUS: File transfer 
failed...");

                    }
                });
Eliza
  • 1
  • 1
  • please explain your answer a little? – Ray May 30 '17 at 10:18
  • Sorry for late reply In my case adding the length to the progress was not enough, but the calculation " (int) (((float)progress * (float)100) / (float)fileSize) " for calculating the progress percentage solved for me. – Eliza Sep 18 '17 at 07:09
0

It worked for me:

    @Override
            public void transferred(int totalTranfered) {

                // Sets the progress indicator to a max value, the
                // current completion percentage, and "determinate"
                // state
                percent = percent + totalTranfered;

                int percentage = (int) ((percent * 100)/ fileSize);
                Log.e("Percentage Transfered", " " + percentage);
                mBuilder.setProgress(100, percentage, false);
                // Displays the progress bar for the first time.
                mNotifyManager.notify(id, mBuilder.build());
                //                            publishProgress(percent);
            }
Vishal Sharma
  • 616
  • 1
  • 6
  • 21