码迷,mamicode.com
首页 > 其他好文 > 详细

显示图片

时间:2017-09-09 10:52:45      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:ace   params   style   syn   sync   try   connect   image   on()   

    private void loadImage(String url, final ImageView ivIcon){


        new AsyncTask<String,Void,Bitmap>(){

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);

                if(bitmap == null){
                    ivIcon.setImageResource(R.mipmap.ic_launcher);
                }else{
                    ivIcon.setImageBitmap(bitmap);
                }

            }

            @Override
            protected Bitmap doInBackground(String... params) {


                try {
                    URL url = new URL(params[0]);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);


                    int code = connection.getResponseCode();
                    if(code == 200){

                        InputStream is = connection.getInputStream();
                        return  BitmapFactory.decodeStream(is);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(url);

 

显示图片

标签:ace   params   style   syn   sync   try   connect   image   on()   

原文地址:http://www.cnblogs.com/yu12/p/7497263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!