how to display external image in android?

前端 未结 2 1797
我在风中等你
我在风中等你 2021-01-03 10:50

I want to display external image like:

\"http://abc.com/image.jpg\"

in my android phone application.

can any one guide me how to achieve this?

相关标签:
2条回答
  • 2021-01-03 10:57

    There are many ways to achieve your request. Basically you have to download the image with an urlrequest and then using the InputStream to create a Bitmap object.

    Just a sample code:

    URL url = new URL("http://asd.jpg");
            URLConnection conn = url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
    
    
            BufferedInputStream bis = new BufferedInputStream(is);
    
            Bitmap bm = BitmapFactory.decodeStream(bis);
    
            bis.close();
            is.close();
    

    After you obtain the Bitmap object you can use it on your ImageView for instance

    0 讨论(0)
  • 2021-01-03 11:11

    Just another approach to download the image from a url

    try {
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://abc.com/image.jpg").getContent());
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    
    0 讨论(0)
提交回复
热议问题