欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > 内容正文

使用HttpUrlConnection来加载网络图片,

来源: 开发者 投稿于  被查看 6988 次 评论:71

使用HttpUrlConnection来加载网络图片,


使用HttpUrlConnection来加载网络图片

    private Context context;
    private Button button;
    private ImageView imageView;
    //加载网络图片需要Bitmap对象
    private Bitmap bitmap;

    public DownloadImageTask(Context context,Button button,ImageView imageView){
        this.context=context;
        this.button=button;
        this.imageView=imageView;
    }


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

            Bitmap bitmap=null;
        try {
            URL url=new URL(strings[0]);
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            InputStream inputStream=httpURLConnection.getInputStream();
            bitmap= BitmapFactory.decodeStream(inputStream);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        imageView.setImageBitmap(bitmap);
    }
相关频道:

用户评论