ImageSwitcher load images from url

2.7k views Asked by At

I'm trying to show images from a URL in a ImageSwitcher. I've tried with:

myImageSwitcher.setImageURI(Uri.parse("http://miurl.com/images/foto01.jpg"));

but it said me this "error":

I/System.outīš• resolveUri failed on bad bitmap uri: http://miurl.com/images/foto01.jpg

Thanks

3

There are 3 answers

0
lokoxumusu On BEST ANSWER

I've solucionated this problem using SliderLayot (https://github.com/daimajia/AndroidImageSlider), but I'll try to do with ImageSwitcher too.

2
Farhan Farooqui On

I know it is late but because I found a better solution for this, would love to share it with all :)

I had to look at the source code of the default ImageSwitcher and know how it works. Whenever we use ImageSwitcher we set a ViewFactory to it. This ViewFactory creates Views which are then used in the ImageSwitcher. Usually we create an ImageView in the ViewFactory. This ImageView is then used in the ImageSwitcher class to draw the view with the image source specified. Below is the setImageResource() method of the ImageSwitcher class.

public void setImageResource(@DrawableRes int resid){
    ImageView image = (ImageView)this.getNextView();
    image.setImageResource(resid);
    showNext();
}

Let's focus on the first and second line of the method.

ImageView image = (ImageView)this.getNextView();
image.setImageResource(resid);

this.getNextView() gets the view from the ViewFactory and casts the view into an ImageView. And then, the Drawable resource is set to it.

Solution

Now, with these information, let's get to the solution! I'm using Volley's NetworkImageView , but you can use Picasso or Glide too! You just have to change the code a bit.

We need to create a custom ImageSwitcher. Here's my code

MyImageSwitcher.java

public class MyImageSwitcher extends ImageSwitcher {

    public MyImageSwitcher(Context context) {
        super(context);
    }

    public MyImageSwitcher(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setImageUrl(String url) {
        NetworkImageView image = (NetworkImageView) this.getNextView();
        image.setImageUrl(url, AppController.getInstance().getImageLoader());
        showNext();
    }
}

Notice the setImageUrl() method. We cast the view we get from getNextView() to a NetworkImageView and set the Url to it. If you want to use Picasso, then just cast it into an ImageView and then use Picasso as it needs to be used.

Activity

MyImageSwitcher imageSwitcher = (MyImageSwitcher) findViewById(R.id.imageSwitcher);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
        public View makeView() {
            NetworkImageView myView = new NetworkImageView(getApplicationContext());
            myView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            myView.setLayoutParams(new ImageSwitcher.LayoutParams(imageSwitcher.getLayoutParams()));
            return myView;
        }
});

If you want to use Picasso, then just use an ImageView.

Layout

<xyz.farhanfarooqui.loadingimages.MyImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

AndroidManifest.xml

Don't forget to include android.permission.INTERNET to your AndroidManifest.xml

Done!

Whenever you want to change the image, do it this way,

imageSwitcher.setImageUrl("http://miurl.com/images/foto01.jpg");
0
Carloso On

I was trying to achieve the same thing when I landed here. I know it's and old question, but since I don't see an answer, I'll tell you the solution that worked for me.

First of all, I downloaded the image/s that I wanted to show in the imageswitcher and return them as a Bitmap using this code:

InputStream in = new java.net.URL(url).openStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);

Then I set the bitmap in the imageswitcher using imageSwitcher.setImageDrawable(new BitmapDrawable(getResources(), bitmap)).

To use java.net.URL(url).openStream(), you'll need to add <uses-permission android:name="android.permission.INTERNET"> to your AndroidManifest.xmlfile.

Have in mind that you could change the download code to make it better (checking for timeouts for example), but this is just a basic solutions to start from.

Another thing about what you were using, I think you can't use setImageURI() to set an internet resource in your ImageSwitcher. Acording to this post, this method is only for content URIs particular to the Android platform (Note that they are talking about an ImageView in that post, but I think the same goes to this case).