2011-09-09 47 views
0

嗨即时通讯工作的一个安卓信使,我需要显示发送和接收文件时的进度条。谁能帮忙?例如,这是我如何发送文件,android进度条在发送文件

@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;   
} 
    } 

现在我在哪里把酒吧,我该怎么做,以显示用户的进展?

+0

我会说你更新你的while循环中的进度;) – Michele

+0

如何:)小白这里:| –

+0

@ hardy.exe:看看我编辑的答案。 – user370305

回答

2

尝试这种::

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(); 

      } 
     } 
    } 

和主要使用::

new xyz().execute(); 
+0

+1准确信息。应该实现AsyncTask。 –

+0

友善的话。但你如何成为粉丝? –

+0

好吧,你看到的东西是,我从另一个类中调用sendFile方法。就像我的消息活动有一个聊天框的权利,所以我点击菜单,并选择发送文件选项。现在调用sendfile方法,其中包含我把我的问题放在代码。现在我需要为该sendfile方法持有另一个类吗?或者我只是把你的建议,因为它的代码? –

0
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); 
     } 

看到this link对于如何构建一个进度条在android系统