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

Android 我想在发送图像时显示进度条,android,android-progressbar,Android,Android Progressbar,我想通过套接字发送图像,在发送过程中,必须显示进度条,并且在发送图像时应更新进度条,但当我尝试此代码时,进度条未显示,图像正在发送。 我试过了,但进度条直接显示100,过程仍在继续,进度条显示100% int count; try { //image send client = new Socket(ServerIP,4444); File file = n

我想通过套接字发送图像,在发送过程中,必须显示进度条,并且在发送图像时应更新进度条,但当我尝试此代码时,进度条未显示,图像正在发送。 我试过了,但进度条直接显示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;
    }

好的,请展示其余的代码,尤其是publishProgress方法。但最有可能的是,您只是忘记了调用setMax。。。当您收到第一个100字节时,它已经是100%。

很抱歉,这是发送图像的实际代码,我忘记了一件事:int count;超乎想象{语句,但如果您愿意,我将放置包含pre、post方法和progressbar详细信息的其余代码…根据您的要求,最大值应该是多少,因为我希望在发送图像时看到进度条从0%进展到100%。实际上,您实现的publishProgress函数对查看如何实际更新ProgressBar
            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;
    }