简体   繁体   English

如何从Android中的URL将图像加载到动态创建的imageview中?

[英]How to load a image into dynamically created imageview from a URL in android?

I have dynamically created imageview in my app. 我已经在我的应用程序中动态创建了imageview。 I tried to load images from URL. 我试图从URL加载图像。 But it does not working. 但这行不通。 I have tried with many methods, and my currently used code is 我尝试了很多方法,目前使用的代码是

 try {
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png").getContent());
            img.setImageBitmap(bitmap);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

But image is not loaded. 但是图像未加载。 What is mistake? 什么错

try piccaso library: 尝试piccaso库:

ImageView imageView = (ImageView) findViewById(R.id.imageView);

//Loading image from below url into imageView //将图片从网址下方加载到imageView中

Picasso.with(this).load("https://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png").into(imageView);

//change url take permission in your mainifest: //在您的清单中更改网址获取权限:

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

for resize: 调整大小:

Picasso.with(getApplicationContext()).load("https://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png").resize(100, 100).into(i1);

You can use Universal Image Loader 您可以使用Universal Image Loader

UIL aims to provide a powerful, flexible and highly customizable instrument for image loading, caching and displaying. UIL旨在为图像加载,缓存和显示提供功能强大,灵活且高度可定制的工具。 It provides a lot of configuration options and good control over the image loading and caching process. 它提供了许多配置选项,并且可以很好地控制图像加载和缓存过程。

// Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view 
//  which implements ImageAware interface)
imageLoader.displayImage(imageUri, imageView);

Reference Link - https://github.com/nostra13/Android-Universal-Image-Loader 参考链接-https: //github.com/nostra13/Android-Universal-Image-Loader

Use library to load images from url 使用库从URL加载图像
1. Glide library Refer here: http://coderzpassion.com/android-working-glide-image-loader-library/ 1. Glide库请参阅此处: http : //coderzpassion.com/android-working-glide-image-loader-library/
2. Picasso library Refer here: http://coderzpassion.com/android-use-picasso-image-loader-library/ 2. Picasso库参考此处: http : //coderzpassion.com/android-use-picasso-image-loader-library/
3. Volley Library Refer here: http://coderzpassion.com/android-working-volley-library/ 3.排球库请参阅此处: http : //coderzpassion.com/android-working-volley-library/

Use Simple AQuery library for image loading from internet 使用Simple AQuery库从Internet加载图像

// AQuery object
private AQuery aquery;

aquery = new AQuery(context);

ImageView imageView = (ImageView) findViewById(R.id.imageView);

aquery.id(imageView).image("Your image url ",false,false);

Add permission to manifest file

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

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

相关问题 如何在Android中从URL加载没有文件扩展名的图像到ImageView - How to load image without file extension from URL to ImageView in Android Android从URL加载图像并显示imageview - Android load image from url and show imageview 在Android中的ImageView中动态加载图像 - Dynamically load Image in ImageView in Android 如何将图像动态添加到动态创建的ImageView - How to dynamically add Image to dynamically created ImageView 如何从android imageview中的dataurl加载图像? - How to load an image from dataurl in android imageview? 如何从类路径加载Android ImageView的图像? - How to load image for Android ImageView from classpath? 如何从 Kotlin 中的 ViewModel 将 URL 中的图像动态加载到 ImageView 中 - How to dynamically load images from a URL into ImageView from a ViewModel in Kotlin 如何使用从动态创建的imageview到活动的图像过渡 - how to use image transition from dynamically created imageview to activity 在 Android(java) 中,如何使用 URL 而不是来自互联网的图像地址在 ImageView 中加载图像? - In Android(java), How to load Image in ImageView by using URL instead of image addresses from the internet? Android:从URL加载ImageView - Android: ImageView load from URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM