[英]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.