hi im working on an android messenger and i need to show the progress bar when sending and receiving files. could anyone help? for instance this is how i send the file,
@Override
public boolean sendFile(String path,String ip, int port) {
// TODO Auto-generated method stub
try {
String[] str = ip.split("\\.");
byte[] IP = new byte[str.length];
for (int i = 0; i < str.length; i++) {
IP[i] = (byte) Integ开发者_开发百科er.parseInt(str[i]);
}
Socket socket = getSocket(InetAddress.getByAddress(IP), port);
if (socket == null) {
Log.i("SO sendFILE","null");
return false;
}
Log.i("SocketOP", "sendFILE-1");
File f = new File(path);
BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );
FileInputStream fileIn = new FileInputStream(f);
Log.i("SocketOP", "sendFILE-2");
byte [] buffer = new byte [(int)f.length()];
System.out.println("SO sendFile f.length();" + f.length());
int bytesRead =0;
while ((bytesRead = fileIn.read(buffer)) > 0) {
out.write(buffer, 0, buffer.length);
System.out.println("SO sendFile" + bytesRead);
}
out.flush();
out.close();
fileIn.close();
Log.i("SocketOP", "sendFILE-3");
} catch (IOException e) {
return false;
//e.printStackTrace();
}
// Toast.makeText(this, "Lvbvhhging...", Toast.LENGTH_SHORT).show();
return true;
}
}
now where do i put the bar and how do i do that inorder to show the user the progress?
try this ::
private class xyz extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(tranning.this);
@Override
protected void onPreExecute() {
this.dialog.setMessage("Please Wait...");
this.dialog.show();
// put your code which preload with processDialog
}
@Override
protected Void doInBackground(Void... arg0) {
// put your code here
Log.i("SocketOP", "sendFILE-1");
File f = new File(path);
BufferedOutputStream out = new BufferedOutputStream( socket.getOutputStream() );
FileInputStream fileIn = new FileInputStream(f);
Log.i("SocketOP", "sendFILE-2");
byte [] buffer = new byte [(int)f.length()];
System.out.println("SO sendFile f.length();" + f.length());
int bytesRead =0;
while ((bytesRead = fileIn.read(buffer)) > 0) {
out.write(buffer, 0, buffer.length);
System.out.println("SO sendFile" + bytesRead);
}
out.flush();
out.close();
fileIn.close();
return null;
}
@Override
protected void onPostExecute(final Void unused) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
and use this in main ::
new xyz().execute();
Do your file sending task in separate thread
not in the UI thread. When you start this thread call your progress dialog and remvove it from thread when you are done using Handler
.
For Handler you can refer this doc:
http://developer.android.com/reference/android/os/Handler.html
myProgressBar.setProgress(0);
while ((bytesRead = fileIn.read(buffer)) > 0) {
out.write(buffer, 0, buffer.length);
//get the previous value of progress bar
int old_value = myProgressBar.getProgress();
//calculate how much did you read from the file
int new_read =(int)( ((float) f.length() / bytesRead) )*100 ) ;
//add the new read to the old_value
int value = new_read+old_value;
myProgressBar.setProgress(value);
System.out.println("SO sendFile" + bytesRead);
}
see this link for how to construct a ProgressBar in android
精彩评论