0

How to display image from URL using in emulator?

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149

2 Answers2

2

how to display image from URL using in emulator?

At first you need to do this in background thread because it requires networking operation that you can't perform on Main(UI) thread. So you can do it in native Thread or use more complex tool for instance AsyncTask. Then you need to connect to URL and fetch data. You have a few options:

First: Retrieve InputStream from URL and create new Drawable:

InputStream inputStream = (InputStream) new URL(yourUrl).getContent();
Drawable drawable = Drawable.createFromStream(inputStream, "srcname");

Or you can use another approach but this requires more code to write so have look at this example.

Note:

Don't forget to add proper permission to your manifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
Community
  • 1
  • 1
Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
0

Try this

URL url = new URL(IMAGE_URL);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70