我想通过套接字发送图像,并且必须显示进度条上的发送过程,并且在发送图像时它应该更新但是当我尝试此代码时,进度条没有显示并且图像正在发送.
try {
//image send
client = new Socket(ServerIP,4444);
File file = new File(path);
byte[] mybytearray = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = client.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(file.getName());
int bytesread=0;
ProgressDialog pd = new ProgressDialog(c);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("Sending...");
pd.setCancelable(false);
pd.setProgress(0);
pd.show();
while((bytesread=fis.read(mybytearray))>0)
os.write(mybytearray, 0, mybytearray.length);
int old_value=pd.getProgress();
int new_read=(int)( ((float)file.length()/bytesread));
int value= new_read+old_value;
pd.setProgress(value);
pd.dismiss();
os.write(mybytearray, 0, mybytearray.length);
os.flush();
bis.close();
fis.close();
client.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}