简体   繁体   中英

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

I have dynamically created imageview in my app. I tried to load images from 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:

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

//Loading image from below url into 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

UIL aims to provide a powerful, flexible and highly customizable instrument for image loading, caching and displaying. 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

Use library to load images from url
1. Glide library Refer here: http://coderzpassion.com/android-working-glide-image-loader-library/
2. Picasso library Refer here: http://coderzpassion.com/android-use-picasso-image-loader-library/
3. Volley Library Refer here: http://coderzpassion.com/android-working-volley-library/

Use Simple AQuery library for image loading from 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" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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