Loading Bitmap in ImageView from url in android

I use the following method to extract a bitmap from url and transfer it to the image, but the image is not updated.

public static Bitmap LoadImageFromWebOperations(String url) { Bitmap bitmap; try { InputStream is = new URL(url).openStream(); bitmap = BitmapFactory.decodeStream(is); return bitmap; } catch (Exception e) { return null; } 

call -

  mov1_poster.setImageBitmap(VPmovies.LoadImageFromWebOperations(mov_details[0][7])); //doesn't work Toast.makeText(this,"url is \n"+mov_details[0][7],Toast.LENGTH_LONG).show(); // shows the url of the image successfully (just to check the url is not null) 

Is there something I'm doing wrong? Please, help.

+2
source share
2 answers
 public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { private ImageView imageView; private Bitmap image; public DownloadImageTask(ImageView imageView) { this.imageView = imageView; } protected Bitmap doInBackground(String... urls) { String urldisplay = urls[0]; try { InputStream in = new java.net.URL(urldisplay).openStream(); image = BitmapFactory.decodeStream(in); } catch (Exception e) { image = null; } return image; } @SuppressLint("NewApi") protected void onPostExecute(Bitmap result) { if (result != null) { imageView.setImageBitmap(result); } } } 

Now call your code:

  new DownloadImageTask(YOUR_IMAGE_VIEW).execute("YOUR_URL"); 
+7
source

Use picasso library when working with images, it works wonders and is easy!

 Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 
+1
source

All Articles