当前位置: 动力学知识库 > 问答 > 编程问答 >

file upload - Android progress bar doesn't update

问题描述:

I'm stuck with a problem with a progress bar. I've created a class which upload a file to an FTP server, the code works correctly but when I tried to implement the progress bar it doesn't update, it jumps from 0% to 100% when the upload is completed.

this is my 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]));

}

}

EDIT:

03-02 17:44:00.030: E/AndroidRuntime(13976): FATAL EXCEPTION: main

03-02 17:44:00.030: E/AndroidRuntime(13976): Process: com.example.downloadingprogressbar, PID: 13976

03-02 17:44:00.030: E/AndroidRuntime(13976): java.lang.NumberFormatException: Invalid int: "0.0"

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): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:648)

03-02 17:44:00.030: E/AndroidRuntime(13976): at 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): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

网友答案:

You are not dividing the numbers properly here:

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

You are using int, so when progress is, say, 1024 bytes and the total length is 4096, the result is 0. Try using float instead.

EDIT: I'd try something like this:

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

Also I think you can avoid unnecessary int -> String -> int. Instead, you could declare your class like this:

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

then

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

and finally

progressDialog.setProgress(values[0]);
分享给朋友:
您可能感兴趣的文章:
随机阅读: