首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > 云计算 >

运用x5cloud云平台来做网络彩讯-文件上传下载进度条使用(八)

2012-06-27 
使用x5cloud云平台来做网络彩讯-------------文件上传下载进度条使用(八)进度条是一个很头疼的问题,当然弄

使用x5cloud云平台来做网络彩讯-------------文件上传下载进度条使用(八)

进度条是一个很头疼的问题,当然弄明白了也很好理解,用起来也很方便,大都会涉及到两个,一个是圆形,另外一个则是长方形,一点一点推进的

对于圆形比较简单,先说说长方形的原理:

?????? 首先需要获取文件大小,在循环写的时候就进行加减乘法运算,哈哈,就这么简单!

给个例子吧:

public class Android_X5_SOSO_9_9_2Activity extends Activity {    /** Called when the activity is first created. */ProgressBar pb;TextView tv;int   fileSize;int   downLoadFileSize;String fileEx,fileNa,filename;private Handler handler = new Handler()  {    @Override    public void handleMessage(Message msg)    {//定义一个Handler,用于处理下载线程与UI间通讯      if (!Thread.currentThread().isInterrupted())      {        switch (msg.what)        {          case 0:            pb.setMax(fileSize);          case 1:            pb.setProgress(downLoadFileSize);            int result = downLoadFileSize * 100 / fileSize;            tv.setText(result + "%");            break;          case 2:            Toast.makeText(Android_X5_SOSO_9_9_2Activity.this, "文件下载完成", 1000).show();            break;           case -1:            String error = msg.getData().getString("error");            Toast.makeText(Android_X5_SOSO_9_9_2Activity.this, error, 1000).show();            break;        }      }      super.handleMessage(msg);    }  };    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                pb=(ProgressBar)findViewById(R.id.down_pb);        tv=(TextView)findViewById(R.id.tv);        new Thread(){        public void run(){        try {        String localpath=Environment        .getExternalStorageDirectory()        .getAbsolutePath()+"/CXSOSO/"        +"20110901005.3gp"; down_file(localpath,"/sdcard/");//下载文件,参数:第一个URL,第二个存放路径} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}        }        }.start();      }    public void down_file(String url,String path) throws IOException{    //下载函数       filename=url.substring(url.lastIndexOf("/") + 1);    //获取文件名    /*URL myURL = new URL(url);    URLConnection conn = myURL.openConnection();    conn.connect();*/    /*InputStream is = conn.getInputStream();    this.fileSize = conn.getContentLength();//根据响应获取文件大小*/        File file = new File(url);      DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));     try {this.fileSize=read(file).length;} catch (Throwable e) {// TODO Auto-generated catch blocke.printStackTrace();}    if (this.fileSize <= 0) throw new RuntimeException("无法获知文件大小 ");    if (is == null) throw new RuntimeException("stream is null");    FileOutputStream fos = new FileOutputStream(path+filename);    //把数据存入路径+文件名    byte buf[] = new byte[1024];    downLoadFileSize = 0;    sendMsg(0);    do      {    //循环读取        int numread = is.read(buf);        if (numread == -1)        {          break;        }        fos.write(buf, 0, numread);        downLoadFileSize += numread;         sendMsg(1);//更新进度条      } while (true);    sendMsg(2);//通知下载完成    try      {        is.close();      } catch (Exception ex)      {        Log.e("tag", "error: " + ex.getMessage(), ex);      }     }private void sendMsg(int flag){    Message msg = new Message();    msg.what = flag;    handler.sendMessage(msg);}  public byte[] read(File myfile) throws Throwable{ //定义输入流,将文件写入到fpath中 DataInputStream inStream = new DataInputStream(new BufferedInputStream(new FileInputStream(myfile)));      byte[] data = readFile(inStream); //     data.length;     return data; }     public static byte[] readFile(InputStream inStream) throws Throwable{      int len = 0;      byte[] buffer = new byte[1024];      ByteArrayOutputStream outStream = new ByteArrayOutputStream();      while((len = inStream.read(buffer))!=-1){          outStream.write(buffer,0,len);      }      outStream.close();      return outStream.toByteArray(); }  }

?

热点排行