1

我想通过套接字发送图像,并且必须显示进度条上的发送过程,并且在发送图像时它应该更新但是当我尝试此代码时,进度条没有显示并且图像正在发送. 我试过了,但是进度条直接显示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;
    }
4

1 回答 1

0

好吧,请显示您的其余代码,尤其是publishProgress方法。但很可能您只是忘记了调用setMax(...),当您收到前 100 个字节时,它已经是 100%。

于 2013-03-23T12:38:54.260 回答