Mtok
Mtok

Reputation: 1630

What is the best way to get an image from URL to ImageView in Android

I am using a method to get the Image from URL when a button is clicked but it gets too much time, about 4-5 seconds to show an image sized only 6 kb using 3G not wi-fi.

My method is this;

public static Bitmap getBitmapFromURL(String src) {
    try {
        Log.e("src",src);
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        Log.e("Bitmap","returned");
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("Exception",e.getMessage());
        return null;
    }
}

and then;

    Bitmap bimage =  getBitmapFromURL("http://www.replikler.net/test2.jpg");
    ImageView image = (ImageView)findViewById(R.id.movie_image);
    image.setImageBitmap(bimage);

do you know another way (faster) to get an image from url ?

Upvotes: 0

Views: 505

Answers (1)

Jug6ernaut
Jug6ernaut

Reputation: 8325

imageView.setImageDrawable(Drawable.createFromStream((InputStream)new URL(url).getContent(), "src"));

Upvotes: 2

Related Questions