简体   繁体   中英

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 ?

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

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