嗨,我在一个android messenger上工作,我需要在发送和接收文件时显示进度条。有人能帮上忙吗?例如,这就是我发送文件的方式,
@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) Integer.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;
}
}现在我该把条放在哪里,我该怎么做才能向用户显示进度呢?
发布于 2011-09-09 18:09:51
试试这个::
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();
}
}
}并在main ::中使用它
new xyz().execute();发布于 2011-09-09 17:56:24
在separate thread中执行文件发送任务,而不是在UI线程中。当你启动这个线程时,调用你的进度对话框,并在使用完Handler后将其从线程中移除。
对于Handler,您可以参考此文档:
http://developer.android.com/reference/android/os/Handler.html
发布于 2011-09-09 18:07:28
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);
}如何在android中构建ProgressBar,请参考this link
https://stackoverflow.com/questions/7359874
复制相似问题