如何在 Android 上显示来自 URL 的图像 [英] How to display image from URL on Android
本文介绍了如何在 Android 上显示来自 URL 的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在屏幕上显示图像.图片应来自 URL,不可绘制.
I want to display image on screen. Image should come from URL, and not drawable.
代码在这里:
<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 的图像?
How do I display image from URL in Android?
推荐答案
您可以直接显示来自网络的图像,无需下载.请检查以下功能.它会将来自网络的图像显示到您的图像视图中.
You can directly show image from web without downloading it. Please check the below function . It will show the images from the web into your image view.
public static Drawable LoadImageFromWebOperations(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return null;
}
}
然后使用活动中的代码将图像设置为 imageview.
then set image to imageview using code in your activity.
这篇关于如何在 Android 上显示来自 URL 的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文