android progressbar正在发送文件

android progressbar正在发送文件,android,Android,嗨,我正在使用android messenger,在发送和接收文件时,我需要显示进度条。有人能帮忙吗?例如,我就是这样发送文件的 @Override public boolean sendFile(String path,String ip, int port) { // TODO Auto-generated method stub try { String[] str = ip.split("\\."); byte[] IP

嗨,我正在使用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;        
}
    }
@覆盖
公共布尔发送文件(字符串路径、字符串ip、int端口){
//TODO自动生成的方法存根
试一试{
字符串[]str=ip.split(\\);
字节[]IP=新字节[str.length];
对于(int i=0;i0){
out.write(buffer,0,buffer.length);
System.out.println(“SO sendFile”+bytesRead);
}
out.flush();
out.close();
fileIn.close();
Log.i(“SocketOP”、“sendFILE-3”);
}捕获(IOE异常){
返回false;
//e、 printStackTrace();
}
//Toast.makeText(这是“Lvbvhhging…”,Toast.LENGTH_SHORT.show();
返回true;
}
}

现在,为了向用户显示进度,我应该将条形图放在哪里?如何做?

单独线程中执行文件发送任务,而不是在UI线程中。启动此线程时,请调用进度对话框,并在使用
处理程序后从线程中将其删除

有关处理程序,您可以参考此文档:


有关如何在android中构建ProgressBar的信息,请参见尝试以下操作:

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

            }   
        }
    }

我想说您在while循环中更新了progressbar;)@哈迪:看看我编辑的答案。谢谢你的好话。但是你怎么会是一个粉丝呢?你看,问题是,我从另一个类调用sendFile方法。就像我的消息传递活动右侧有一个聊天框一样,因此我单击菜单并选择sendfile选项。现在调用sendfile方法,其中包含我在问题中输入的代码。现在我需要为sendfile方法创建另一个类来保存吗?或者我只是把你建议的代码放在原处吗post@hardy.exe你可以照原样继续,非常感谢。一个简单的问题:我在哪里调用xyz().execute?调用sendFile方法的UI?
 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();