如何在模拟器中使用URL
显示图像?
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 .
Don't forget to add proper permission to your manifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
Try this
URL url = new URL(IMAGE_URL);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);
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.