Load image from url

前端 未结 16 2020
独厮守ぢ
独厮守ぢ 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:45

    Here is sample code for display Image from URL.

    public static Void downloadfile(String fileurl, ImageView img) {
            Bitmap bmImg = null;
            URL myfileurl = null;
            try {
                myfileurl = new URL(fileurl);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            try {
                HttpURLConnection conn = (HttpURLConnection) myfileurl.openConnection();
                conn.setDoInput(true);
                conn.connect();
                int length = conn.getContentLength();
                if (length > 0) {
                    int[] bitmapData = new int[length];
                    byte[] bitmapData2 = new byte[length];
                    InputStream is = conn.getInputStream();
                    bmImg = BitmapFactory.decodeStream(is);
                    img.setImageBitmap(bmImg);
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

提交回复
热议问题