0
嗨,我想添加一個進度條到我的活動,不知道如何做到這一點。這裏是場景: 我的活動消息調用IMSERVICE.SENDFILE(),我將消息的上下文作爲參數傳遞給IMSERVICE.SENDFILE()。 IMSERVICE.SENDFILE()調用SOCKETOPERATOR.SENDFILE()。我也通過相同的背景。現在安卓進度條
public boolean sendFile(Context c,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","");
return false;
}
Log.i("SocketOP", "sendFILE-1");
File f = new File(path);
String filename=path.substring(path.lastIndexOf("/")+1);
System.out.println("filename:"+filename);
fin.filename = "~"+filename;
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;
ProgressDialog pbarDialog = new ProgressDialog(c);
pbarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pbarDialog.setMessage("Loading...");
pbarDialog.setCancelable(false);
pbarDialog .setProgress(0);
pbarDialog.show();
while ((bytesRead = fileIn.read(buffer)) > 0) {
out.write(buffer, 0, buffer.length);
//get the previous value of progress bar
int old_value = pbarDialog .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;
pbarDialog.setProgress(value);
Log.i("SocketOP", "sendFILE-3");
System.out.println("SO sendFile" + bytesRead +filename);
}
pbarDialog.dismiss();
out.flush();
out.close();
fileIn.close();
} catch (IOException e) {
return false;
//e.printStackTrace();
}
// Toast.makeText(this, "Lvbvhhging...", Toast.LENGTH_SHORT).show();
return true;
}
,你可以看到我有它一個進度:SOCKETOPERATOR.SENDFILE()具有發送文件的方法和看起來像這樣。我不知道如何使它從我的消息活動中顯示出來。另外,我不知道我是否正確使用了progressDilog。任何人都可以幫忙嗎?
這是第三次你問這個問題。爲什麼? – Michele
因爲它而掙扎。在我最後的帖子上有人說如果答案沒有幫助提出一個新問題.. –