我想在发送图像时显示进度条

时间:2013-03-23 12:34:39

标签: android android-progressbar

我想通过套接字发送图像,同时必须显示进度条上的发送过程,并且应该在图像发送时更新但是当我尝试此代码时,未显示进度条和图像正在发送。 我试过这个,但进度条直接显示100,进程仍在进行,进度条显示100%

            int count;         
            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()); 
            long total = 0;

            while ((count = bis.read(mybytearray)) != -1) {
                total += count;
                publishProgress(""+(int)((total*100)/file.length()));
                 os.write(mybytearray, 0, mybytearray.length);
                 //os.write(mybytearray, 0, mybytearray.length);

            }
           // os.write(mybytearray, 0, mybytearray.length);
           // 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();
        }

        return null;
    }

1 个答案:

答案 0 :(得分:0)

嗯,请显示其余代码,尤其是publishProgress方法。但很可能你只是忘记拨打setMax(...)并且当你收到前100个字节时它已经是100%。

相关问题