Load image from url

前端 未结 16 2007
独厮守ぢ
独厮守ぢ 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();
            }
        }
    
    0 讨论(0)
  • 2020-11-22 05:45

    add Internet permission in manifest

    <uses-permission android:name="android.permission.INTERNET" />
    

    than create methode as below,

     public static Bitmap getBitmapFromURL(String src) {
        try {
            Log.e("src", src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            Log.e("Bitmap", "returned");
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Exception", e.getMessage());
            return null;
        }
    }
    

    now add this in your onCreate method,

     ImageView img_add = (ImageView) findViewById(R.id.img_add);
    
    
    img_add.setImageBitmap(getBitmapFromURL("http://www.deepanelango.me/wpcontent/uploads/2017/06/noyyal1.jpg"));
    

    this is works for me.

    0 讨论(0)
  • 2020-11-22 05:46

    There is two way :

    1) Using Glide library This is best way to load image from url because when you try to display same url in second time it will display from catch so improve app performance

    Glide.with(context).load("YourUrl").into(imageView);
    

    dependency : implementation 'com.github.bumptech.glide:glide:4.10.0'


    2) Using Stream. Here you want to create bitmap from url image

    URL url = new URL("YourUrl");
    Bitmap bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    imageView.setImageBitmap(bitmap);
    
    0 讨论(0)
  • 2020-11-22 05:50

    UrlImageViewHelper will fill an ImageView with an image that is found at a URL. UrlImageViewHelper will automatically download, save, and cache all the image urls the BitmapDrawables. Duplicate urls will not be loaded into memory twice. Bitmap memory is managed by using a weak reference hash table, so as soon as the image is no longer used by you, it will be garbage collected automatically.

    UrlImageViewHelper.setUrlDrawable(imageView, "http://example.com/image.png");

    https://github.com/koush/UrlImageViewHelper

    0 讨论(0)
提交回复
热议问题