显示图片

    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);

 

posted on 2017-09-09 09:54  权威的程序  阅读(354)  评论(0编辑  收藏  举报