Android进度条不会更新

时间:2015-03-02 16:30:10

标签: android file-upload progress-bar

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

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):致命异常:主要 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:无效的int:&#34; 0.0&#34; 03-02 17:44:00.030:E / AndroidRuntime(13976):at java.lang.Integer.invalidInt(Integer.java:138) 03-02 17:44:00.030:E / AndroidRuntime(13976):at java.lang.Integer.parse(Integer.java:410) 03-02 17:44:00.030:E / AndroidRuntime(13976):at java.lang.Integer.parseInt(Integer.java:367) 03-02 17:44:00.030:E / AndroidRuntime(13976):at java.lang.Integer.parseInt(Integer.java:334) 03-02 17:44:00.030:E / AndroidRuntime(13976):at com.example.downloadingprogressbar.MainActivity $ Upload.onProgressUpdate(MainActivity.java:142) 03-02 17:44:00.030:E / AndroidRuntime(13976):at 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):at java.lang.reflect.Method.invoke(Native Method) 03-02 17:44:00.030:E / AndroidRuntime(13976):at java.lang.reflect.Method.invoke(Method.java:372) 03-02 17:44:00.030:E / AndroidRuntime(13976):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) 03-02 17:44:00.030:E / AndroidRuntime(13976):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:1)

您没有在这里正确划分数字:

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

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

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

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

另外我认为你可以避免不必要的int - &gt;字符串 - &gt; INT。相反,你可以这样声明你的类:

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

然后

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

最后

progressDialog.setProgress(values[0]);