Quontas
Quontas

Reputation: 399

Updating ImageView based on URL

I have spent a lot of time trolling through multiple different threads concerning this topic, but I have yet to find an answer that works well with my code (Android SDK 23, in 2016). A lot of the answers are deprecated, and others just flat-out don't work like they're supposed to, and I was wondering if I could get a solid answer on this:

I am trying to include a Pokemon sprite (static image) in my program from Serebii. nums is a variable indicating the Pokemon's dex number (this one functions correctly, I promise). And this code is running in the main UI thread, which I know is frowned upon, but right now I'm trying to get the image loading, and then the smoothness of the app down. I don't really need a Bitmap, per se, but I need my ImageView to update and display the image given by the URL. How do I do it?

URL url = null;
try {
     url = new URL("http://www.serebii.net/xy/pokemon/" + nums + ".png");
} catch (MalformedURLException e) {
  e.printStackTrace();
}
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
mImageView.setImageBitmap(bmp);

Upvotes: 0

Views: 134

Answers (4)

Ravindra Kushwaha
Ravindra Kushwaha

Reputation: 8042

Use the Piassco library for it..Library link

For setting the Bitmap on the ImageView

Picasso.with(getContext()).load("your url").into(new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            //do what ever you want with your bitmap 
          imgView.setImageBitmap(loadedImage);///imgView is use to set the image in it
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    });

And another method of Picasso :-

Picasso.with(this)
        .load("your_image_url")
        .placeholder(R.drawable.no_image)
        .error(android.R.drawable.stat_notify_error)
        .networkPolicy(NetworkPolicy.OFFLINE)//user this for offline support
        .into(YOUR_IMAEGVIEW);

OR

You can also use the Universal Image Loader..Here is the link Universal image loader

ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance
            ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(getActivity()));
            imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
              DisplayImageOptions options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(R.drawable.YOUR_DRAWABLE)
                    .showImageForEmptyUri(R.drawable.YOUR_DRAWABLE)
                    .showImageOnFail(R.drawable.YOUR_DRAWABLE)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .considerExifParams(true)
                    .build();

imageLoader.displayImage("your_image_url", YOUR_IMAGEVIEW, null);

Upvotes: 0

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

You can try using Picasso as:

Picasso.with(context)
          .load(url)
          .into(imageview);

Or use Universal Image loader as:

 ImageLoader imageLoader = new  ImageLoader(context);
 imageLoader.displayImage(imageUri, imageView);

Upvotes: 0

Deepak Goyal
Deepak Goyal

Reputation: 4917

You can use Picasso Library to load images.
a) Add Gradle into your project.

compile 'com.squareup.picasso:picasso:2.5.2'

b) Usage

Picasso.with(context).load("http://www.serebii.net/xy/pokemon/" + nums + ".png").into(imageView);

Also there are another libraries, you can use like Fresco by Facebook, Universal Image loader

Upvotes: 0

Neerajlal K
Neerajlal K

Reputation: 6828

Just use Picasso library, it will do all your image loading. You only need to provide the url of the image correctly.

String url = "http://www.serebii.net/xy/pokemon/" + nums + ".png";

Picasso.with(yourContext)
    .load(url)
    .into(mImageView);

Upvotes: 1

Related Questions