2

I need to get images from a server and load it to image-button.There are more than 50 images dynamically loading when application fragment starts. What is the best way to do this.Currently I'm using following code and when I run it application is stuck and also I followed Android Loading Image from URL (HTTP) tutorial also, but it is not showing images when there are more than one images.

this is the code I’m currently using when but application is freezing

private void setListParentItemInfo(View convertView,final IPTVChannel iptvChannel){
 ImageButton logoBtn = ((ImageButton) convertView.findViewById(R.id.channelLogo));

        String image_url="http://Channel.com.au/ChannelLogos/"+Channel.getLogoName();
  logoBtn.setImageBitmap(downloadBitmap(image_url));
}


private Bitmap downloadBitmap(String url) {
    // initilize the default HTTP client object
    final DefaultHttpClient client = new DefaultHttpClient();

    //forming a HttoGet request
    final HttpGet getRequest = new HttpGet(url);
    try {

        HttpResponse response = client.execute(getRequest);

        //check 200 OK for success
        final int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode != HttpStatus.SC_OK) {
            Log.w("ImageDownloader", "Error " + statusCode +
                    " while retrieving bitmap from " + url);
            return null;

        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                // getting contents from the stream
                inputStream = entity.getContent();

                // decoding stream data back into image Bitmap that android understands
                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                return bitmap;
            } finally {
                if (inputStream != null) {
                    inputStream.close();
                }
                entity.consumeContent();
            }
        }
    } catch (Exception e) {
        // You Could provide a more explicit error message for IOException
        getRequest.abort();
        Log.e("ImageDownloader", "Something went wrong while" +
                " retrieving bitmap from " + url + e.toString());
    }

    return null;
}

How can I fix this or any other why to do this?

Nuwan Indika
  • 901
  • 4
  • 14
  • 27

6 Answers6

4

Use Picasso Library

private void setListParentItemInfo(View convertView,final IPTVChannel iptvChannel){
 ImageButton logoBtn = ((ImageButton) convertView.findViewById(R.id.channelLogo));

 String image_url="http://Channel.com.au/ChannelLogos/"+Channel.getLogoName();
 Picasso.with(context).load(image_url).placeholder(yourDrawableImage).into(logoBtn);
}
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
3

use picaso library , it's easy to use and fast and save cash for further uses.

http://square.github.io/picasso/

Pirisok
  • 401
  • 3
  • 9
2

Yes, it's freezing because it's running in the same thread as UI (main thread). Always use different thread (eg. AsyncTask) for loading something from network, newer API levels shouldn't allow you to do this at all. But definitely you should go with the Picasso library in this case.

Vojtěch Sázel
  • 538
  • 6
  • 22
2

If your using small images then you can load images by using async task like below. If you have large images then you need to follow http://developer.android.com/intl/es/training/displaying-bitmaps/load-bitmap.html

class ImageLoader extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... params) {
        return getBitmapFromURL(params[0]);
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        imageButton.setImageBitmap(result);
    }

}

public Bitmap getBitmapFromURL(String imageURL) {
    try {
        URL url = new URL(imageURL);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        // Log exception
        return null;
    }
}
Raja Peela
  • 1,366
  • 2
  • 14
  • 26
0

I suppose you should use multiple thread per each image like it show in the How to use separate thread to perform http requests

Community
  • 1
  • 1
Mikhail Vitik
  • 506
  • 7
  • 10
0

Use Libraries like fresco by facebook. http://frescolib.org/

Using this you dont need to download the images externally. Just set the image passing uri. It will automatically download and set it to the view.

Er.Rohit Sharma
  • 696
  • 5
  • 21