Skip to content
Advertisement

How to load image from URL in Fragment class?

I want to load an image from its URL in Fragment class. I’m loading the image like this:

try {
 // ImageView j=(ImageView)rootView.findViewById(R.id.image_frm);
  Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(image_url).getContent());
  Bitmap resized = Bitmap.createScaledBitmap(bitmap, 600, 600, true);
  Bitmap conv_bm = getRoundedRectBitmap(resized, 255);
  i.setImageBitmap(conv_bm); 
 // j.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

but this method is taking more time to load the image and to open it. If I want to use ImageLoader class like this tutorial, then how to load it in Fragment?

Advertisement

Answer

Use any 3rd party library like Picasso. Download the .jar file and put the it inside your libs folder. For more info see Picasso’s documentation.

in case of Fragments, use getActivity() instead of context.

For rounded ImageView, I use the RoundedImageView lib.

Advertisement