Load image from url

前端 未结 16 1999
独厮守ぢ
独厮守ぢ 2020-11-22 05:12

I have an image URL. I want to display an image from this URL in an ImageView but I am unable to do that.

How can this be achieved?

16条回答
  •  别跟我提以往
    2020-11-22 05:35

    loadImage("http://relinjose.com/directory/filename.png");
    

    Here you go

    void loadImage(String image_location) {
        URL imageURL = null;
        if (image_location != null) {
            try {
                imageURL = new URL(image_location);         
                HttpURLConnection connection = (HttpURLConnection) imageURL
                        .openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream inputStream = connection.getInputStream();
                bitmap = BitmapFactory.decodeStream(inputStream);// Convert to bitmap
                ivdpfirst.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            //set any default
        }
    }
    

提交回复
热议问题