HttpURLConnection一个适配器加载图片的类

来源:互联网 发布:数控铣削平面编程实例 编辑:程序博客网 时间:2024/06/05 20:07
public void dispalyImage(final ImageView img, final String path){    AsyncTask<Void,Void,Bitmap> asyncTask=new AsyncTask<Void, Void, Bitmap>() {        @Override        protected Bitmap doInBackground(Void... voids) {            try {                URL url = new URL(path);                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();                urlConnection.setRequestMethod("GET");                urlConnection.setConnectTimeout(5000);                urlConnection.setReadTimeout(5000);                int responseCode = urlConnection.getResponseCode();                if (responseCode==200){                    InputStream inputStream = urlConnection.getInputStream();                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);                    return  bitmap;                }            } catch (Exception e) {                e.printStackTrace();            }            return null;        }        @Override        protected void onPostExecute(Bitmap bitmap) {                img.setImageBitmap(bitmap);        }    };    asyncTask.execute();}
原创粉丝点击