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

android进度条
EN

Stack Overflow用户
提问于 2011-09-11 05:42:12
回答 2查看 468关注 0票数 0

嗨,谁能告诉我如何在这个方法中添加一个进度条:

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

                return false;
            }

            Log.i("SocketOP", "sendFILE-1");
            File  f = new File(path);
            String filename=path.substring(path.lastIndexOf("/")+1);
            System.out.println("filename:"+filename); 
            fin.filename = "~"+filename;
            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 +filename);
            }
            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

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-11 05:46:09

代码语言:javascript
复制
ProgressDialog pbarDialog =  new ProgressDialog( mContext ); 
pbarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pbarDialog.setMessage("Loading...");
pbarDialog.setCancelable(false);
pbarDialog.setMax(100);
 pbarDialog .setProgress(0);
 pbarDialog.show();
    while ((bytesRead = fileIn.read(buffer)) > 0) {
                    out.write(buffer, 0, buffer.length);
      //get the previous value of progress bar 
                    int old_value = pbarDialog .getProgress();
                    //calculate how much did you read from the file
                    int new_read =(int)( ((float)  bytesRead/f.length()) )*100 ) ;
                    //add the new read to the old_value
                    int value = new_read+old_value;
                    pbarDialog.setProgress(value);              
      System.out.println("SO sendFile" + bytesRead +filename);
                }
 pbarDialog.dismiss();
票数 1
EN

Stack Overflow用户

发布于 2011-09-11 05:43:45

您需要从UI线程运行该函数才能显示进度条,但是在函数完成之前,您将无法看到任何进度。对于您正在尝试做的事情,正确的方法是AsyncTask,请检查

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

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

https://stackoverflow.com/questions/7374882

复制
相关文章

相似问题

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