如何在Android上显示来自URL的图像

如何在Android上显示来自URL的图像

我想在屏幕上显示图像。图像应来自URL,而不是可绘制的。

代码在这里:

<ImageView android:id="@+id/ImageView01" android:src = "http://l.yimg.com/a/i/us/we/52/21.gif"
    android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>

但它在编译时出错。

如何在Android中显示来自URL的图像?


www说
浏览 658回答 3
3回答

跃然一笑

您可以直接在网上显示图像而无需下载。请检查以下功能。它会将网络上的图像显示在图像视图中。public&nbsp;static&nbsp;Drawable&nbsp;LoadImageFromWebOperations(String&nbsp;url)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;InputStream&nbsp;is&nbsp;=&nbsp;(InputStream)&nbsp;new&nbsp;URL(url).getContent(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Drawable&nbsp;d&nbsp;=&nbsp;Drawable.createFromStream(is,&nbsp;"src&nbsp;name"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;d; &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;null; &nbsp;&nbsp;&nbsp;&nbsp;}}然后使用活动中的代码将图像设置为imageview。

动漫人物

您可以尝试这个我在另一个问题中找到的。Android,在URL上创建一个等于ImageView图像的图像try&nbsp;{ &nbsp;&nbsp;ImageView&nbsp;i&nbsp;=&nbsp;(ImageView)findViewById(R.id.image); &nbsp;&nbsp;Bitmap&nbsp;bitmap&nbsp;=&nbsp;BitmapFactory.decodeStream((InputStream)new&nbsp;URL(imageUrl).getContent()); &nbsp;&nbsp;i.setImageBitmap(bitmap);&nbsp;}&nbsp;catch&nbsp;(MalformedURLException&nbsp;e)&nbsp;{ &nbsp;&nbsp;e.printStackTrace();}&nbsp;catch&nbsp;(IOException&nbsp;e)&nbsp;{ &nbsp;&nbsp;e.printStackTrace();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android