首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android发送文件的进度条

android发送文件的进度条
EN

Stack Overflow用户
提问于 2011-09-09 17:52:08
回答 3查看 1.7K关注 0票数 0

嗨,我在一个android messenger上工作,我需要在发送和接收文件时显示进度条。有人能帮上忙吗?例如,这就是我发送文件的方式,

代码语言:javascript
复制
@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;        
}
    }

现在我该把条放在哪里,我该怎么做才能向用户显示进度呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-09 18:09:51

试试这个::

代码语言:javascript
复制
 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();

            }   
        }
    }

并在main ::中使用它

代码语言:javascript
复制
    new xyz().execute();
票数 2
EN

Stack Overflow用户

发布于 2011-09-09 17:56:24

separate thread中执行文件发送任务,而不是在UI线程中。当你启动这个线程时,调用你的进度对话框,并在使用完Handler后将其从线程中移除。

对于Handler,您可以参考此文档:

http://developer.android.com/reference/android/os/Handler.html

票数 0
EN

Stack Overflow用户

发布于 2011-09-09 18:07:28

代码语言:javascript
复制
myProgressBar.setProgress(0);
while ((bytesRead = fileIn.read(buffer)) > 0) {
            out.write(buffer, 0, buffer.length);
            //get the previous value of progress bar 
            int old_value = myProgressBar.getProgress();
            //calculate how much did you read from the file
            int new_read =(int)( ((float) f.length() / bytesRead) )*100 ) ;
            //add the new read to the old_value
            int value = new_read+old_value;
            myProgressBar.setProgress(value); 
            System.out.println("SO sendFile" + bytesRead);
        }

如何在android中构建ProgressBar,请参考this link

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7359874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档