代码片--Android--图片下载进度条

public class MainActivity extends ActionBarActivity {

    Button b;
    ImageView i;
    ProgressDialog p;//进度条
    String path="http://pic.nipic.com/2007-12-23/200712231523651_2.jpg";//图片地址

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b = (Button) findViewById(R.id.button1);
        i = (ImageView) findViewById(R.id.imageView1);
        p = new ProgressDialog(this);
        p.setTitle("提示信息");
        p.setMessage("下载中,请稍后...");
        p.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//设置进度样式
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new MyTask().execute(path);//启动AsyncTask
            }
        });

    }

//第一个参数是路径
//第二个参数是进度刻度
//但三个参数是返回类型
    class MyTask extends AsyncTask<String, Integer, Bitmap> {

//准备阶段,一般显示进度条
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            p.show();
        }
//后台长时间跑的逻辑业务
        @Override
        protected Bitmap doInBackground(String... params) {
            // TODO Auto-generated method stub
            Bitmap bp=null;
            InputStream in=null;
            ByteArrayOutputStream bo=new ByteArrayOutputStream();
            //设置Http协议访问
            HttpClient httpClient=new DefaultHttpClient();
            HttpGet httpGet =new HttpGet(params[0]);
            try {
                HttpResponse httpResponse=httpClient.execute(httpGet);

                //当Http请求成功返回
                if(httpResponse.getStatusLine().getStatusCode()==200){
                    in=httpResponse.getEntity().getContent();//通过流得到实体内容
                    long file_length=httpResponse.getEntity().getContentLength();//得到文件总容量
                    int len=0;//每次读取长度
                    int total_length=0;//已读取总长
                    byte[] data=new byte[1024];
                    while ((len=in.read(data))!=-1) {
                        total_length+=len;
                        int value= (int) ((total_length/(float)file_length)*100);
                        publishProgress(value);
                        bo.write(data, 0, len);
                    }
                    byte[] result=bo.toByteArray();
                    bp=BitmapFactory.decodeByteArray(result, 0, result.length);

                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
//对各种流关闭
            finally{
                if(in!=null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                if(bo!=null){
                    try {
                        bo.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }

            return bp;
        }
//时时更新UI进度条
        @Override
        protected void onProgressUpdate(Integer... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
            p.setProgress(values[0]);
        }
//doInBackground方法结束后执行此方法,对UI做最后更新
        @Override
        protected void onPostExecute(Bitmap result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            p.dismiss();
            i.setImageBitmap(result);
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值