简体   繁体   English

Android:从URL加载ImageView

[英]Android: ImageView load from URL

Tried many ways in the internet but my imageView still display blank of the image. 在Internet中尝试了许多方法,但是我的imageView仍然显示图像空白。 Wish to retrieve the image from url (download or refer it) but seems I missed out something. 希望从URL(下载或引用)中检索图像,但似乎我错过了一些东西。 I have activated the INTERNET permission in manifest. 我已激活清单中的INTERNET权限。

 icon.setImageResource(R.drawable.portrait_user);

        try {
            URL url = new URL("https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpf1/t1.0-1/c0.0.50.50/p50x50/1499583_10202305028778787_1063740680_n.jpg");
            InputStream content = (InputStream)url.getContent();
            Bitmap d = BitmapFactory.decodeStream(content);
            icon.setImageBitmap(d);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Doing network I/O on the main application thread like this will crash your app on Android 4.0+. 像这样在主应用程序线程上执行网络I / O操作会使Android 4.0+上的应用程序崩溃。

Most likely, you will be better served using a third-party library that can download the image for you in the background and update the ImageView when it is ready. 最有可能的是,使用第三方库可以为您提供更好的服务,该库可以在后台为您下载图像并在就绪后更新ImageView Picasso and Universal Image Loader are two of the more popular libraries for this. 毕加索(Picasso)通用图像加载器(Universal Image Loader)是两个比较流行的库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM