【问题标题】:I want to show Progress Bar while Sending the image我想在发送图像时显示进度条
【发布时间】:2013-03-23 12:34:38
【问题描述】:

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

【问题讨论】:

    标签: android android-progressbar


    【解决方案1】:

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

    【讨论】:

    • 抱歉,这是发送图像的实际代码,我忘记的一件事是 int count;在 try{ 语句上方,但如果您愿意,我将放置包含 pre、post 方法和进度条详细信息的其余代码...
    • 根据您的说法,最大值应该是多少,因为我希望在发送图像时看到进度条从 0 到 100 %
    • 实际上你实现的 publishProgress 函数会很有用,看看你是如何真正更新 ProgressBar
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多