Android 进度条不更新

Android progress bar doesn't update

我遇到了进度条问题。我创建了一个 class 将文件上传到 FTP 服务器,代码工作正常但是当我尝试实现进度条时它不会更新,它从 0% 跳到 100%上传完成后。 这是我的 class:

class Upload extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        FTPClient client= new FTPClient();
        try {
                InetAddress indirizzo = InetAddress.getByName("myurl.com");
                client.connect(indirizzo);
                //risposta.setText(client.getReplyString());
                //aperta la connessione al server procedo al login
                boolean loggato=client.login("username", "password");
                //if(loggato==true) risposta.append(" ....login effettuato");
                //else risposta.append("...login fallito");
                client.enterLocalPassiveMode();
                //apro il file contenuto nel path /sdcard come stream
                client.setFileType(FTP.BINARY_FILE_TYPE); // imposto il tipo di dati, da modificare a seconda dell'utilità
                File sdcardDir = Environment.getExternalStorageDirectory(); //dato che il mio file era sulla sd prendo il riferimento alla memoria       esterna
                File file = new File(sdcardDir,"fotografia.jpg"); //prendo il riferimento al file salvato sulla sd
                FileInputStream fis = new FileInputStream(file);//creo uno stream in input a partire dal mio file
                long lengthofFile = file.length();
                //risposta.append("...path file: " + file.getPath()); //questa istruzione la usavo per test, la lascio casomai dovesse servire, stampa semplicemente il path del file che voglio uploadare

                boolean upload_ok=client.storeFile("fotografia.jpg", fis);
                int progress = 0;
                int bytesRead = 0;
                byte buf[] = new byte[1024];
                BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(file));
                while ((bytesRead = bufInput.read(buf)) != -1) {
                  progress += bytesRead;
                  publishProgress("" + (int) ((progress * 100) / lengthofFile));
                }
                fis.close();//procedo alla chiusura dello stream, infatti storeFile non chiude fis


                } catch (SocketException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        return null;
    }
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        dismissDialog(DIALOG_UPLOAD_PROGRESS);
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        showDialog(DIALOG_UPLOAD_PROGRESS);
    }

    @Override
    protected void onProgressUpdate(String... values) {
        // TODO Auto-generated method stub
        //Log.d("ANDRO_ASYNC", values[0]);
        progressDialog.setProgress(Integer.parseInt(values[0]));
    }

}

编辑:

03-02 17:44:00.030: E/AndroidRuntime(13976): 致命异常: main 03-02 17:44:00.030: E/AndroidRuntime(13976): 过程: com.example.downloadingprogressbar, PID: 13976 03-02 17:44:00.030:E/AndroidRuntime(13976):java.lang.NumberFormatException:无效整数:“0.0” 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 java.lang.Integer.invalidInt(Integer.java:138) 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 java.lang.Integer.parse(Integer.java:410) 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 java.lang.Integer.parseInt(Integer.java:367) 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 java.lang.Integer.parseInt(Integer.java:334) 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 com.example.downloadingprogressbar.MainActivity$Upload.onProgressUpdate(MainActivity.java:142) 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 com.example.downloadingprogressbar.MainActivity$Upload.onProgressUpdate(MainActivity.java:1) 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:648) 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 android.os.Handler.dispatchMessage(Handler.java:102) 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 android.os.Looper.loop(Looper.java:135) 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 android.app.ActivityThread.main(ActivityThread.java:5221) 03-02 17:44:00.030:E/AndroidRuntime(13976):在 java.lang.reflect.Method.invoke(本机方法) 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 java.lang.reflect.Method.invoke(Method.java:372) 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 03-02 17:44:00.030: E/AndroidRuntime(13976): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

你没有正确划分这里的数字:

publishProgress("" + (int) ((progress * 100) / lengthofFile));

您正在使用 int,因此当进度为 1024 字节且总长度为 4096 时,结果为 0。请尝试改用 float。

编辑: 我会尝试这样的事情:

...
publishProgress((int) (((float)progress * (float)100) / (float)lengthofFile));
...

另外我认为你可以避免不必要的 int -> String -> int。相反,您可以这样声明 class:

class Upload extends AsyncTask<String, int, String> {

然后

publishProgress((int) (((float)progress * (float)100) / (float)lengthofFile));

最后

progressDialog.setProgress(values[0]);