我遇到了進度條問題。我創建了一個將文件上傳到FTP服務器的類,該代碼正常工作,但是當我試圖實現進度條時,它不會更新,當上傳完成時,它會從0%跳到100%。 這是我的類:Android進度條不更新
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:電子/ AndroidRuntime(13976):進程:com.example.downloadingprogressbar,PID:13976 03-02 17:44:00.030:E/AndroidRuntime(13976):java.lang.NumberFormatException:無效int:「0.0」012-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):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在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):at android.os.Looper.loop(Looper.java:135) 03-02 17:44:00.030:E/AndroidRuntime(13976):at 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 ):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
我改變的int浮動,但現在它給我這個錯誤(檢查編輯答案)當上傳完成時,進度條仍然沒有更新。 – Leonardo 2015-03-02 16:47:13
這似乎是因爲你仍然在最後一個方法中使用Integer.parseInt。 – 2015-03-02 16:51:14
我該如何改變它? – Leonardo 2015-03-02 16:58:44