如何在Android中通过URL加载ImageView?
如何在 ImageView
中使用 URL 引用的图像?
How do you use an image referenced by URL in an ImageView
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何在 ImageView
中使用 URL 引用的图像?
How do you use an image referenced by URL in an ImageView
?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(23)
我最近发现了一个线程这里< /a>,因为我必须对带有图像的列表视图执行类似的操作,但原理很简单,您可以在其中显示的第一个示例类中阅读(由 jleedev 编写)。
您获取图像的输入流(来自网络)
然后将图像存储为Drawable,然后可以将其传递给ImageView(通过setImageDrawable)。再次从上面的代码片段看一下整个线程。
I have recently found a thread here, as I have to do a similar thing for a listview with images, but the principle is simple, as you can read in the first sample class shown there (by jleedev).
You get the Input stream of the image (from web)
Then you store the image as Drawable and you can pass it to the ImageView (via setImageDrawable). Again from the upper code snippet take a look at the entire thread.
这是一个迟到的答复,正如上面所建议的
AsyncTask
将会,在谷歌搜索了一下之后,我发现了解决这个问题的另一种方法。Drawable drawable = Drawable.createFromStream((InputStream) new URL("url").getContent(), "src");
imageView.setImageDrawable(drawable);
这里是完整功能:
不要忘记在您的
AndroidManifest.xml
中添加以下权限来访问互联网。我自己尝试过,但还没有遇到任何问题。
This is a late reply, as suggested above
AsyncTask
will will and after googling a bit i found one more way for this problem.Drawable drawable = Drawable.createFromStream((InputStream) new URL("url").getContent(), "src");
imageView.setImageDrawable(drawable);
Here is the complete function:
Don't forget to add the following permissions in your
AndroidManifest.xml
to access the internet.<uses-permission android:name="android.permission.INTERNET" />
I tried this myself and i have not face any issue yet.
这里有很多好的信息...我最近发现一个名为 SmartImageView 的类,到目前为止似乎运行得很好。非常容易合并和使用。
http://loopj.com/android-smart-image-view/
< a href="https://github.com/loopj/android-smart-image-view" rel="nofollow noreferrer">https://github.com/loopj/android-smart-image-view
更新:我最终写了一个 有关此内容的博客文章,因此请查看它以获取有关使用 SmartImageView 的帮助。
第二次更新:我现在总是使用毕加索(见上文)并强烈推荐它。 :)
Lots of good info in here...I recently found a class called SmartImageView that seems to be working really well so far. Very easy to incorporate and use.
http://loopj.com/android-smart-image-view/
https://github.com/loopj/android-smart-image-view
UPDATE: I ended up writing a blog post about this, so check it out for help on using SmartImageView.
2ND UPDATE: I now always use Picasso for this (see above) and highly recommend it. :)
这将帮助您...
定义 imageview 并将图像加载到其中...
然后定义此方法:
This will help you...
Define imageview and load image into it .....
Then Define this method :
具有异常处理和异步任务的版本:
Version with exception handling and async task:
一种简单而干净的方法是使用开源库 Prime。
A simple and clean way to do this is to use the open source library Prime.
适用于任何容器中的 imageView,例如 listview 网格视图、普通布局
Working for imageView in any container , like listview grid view , normal layout
尝试这种方式,希望这能帮助您解决问题。
这里我解释如何使用“AndroidQuery”外部库以asyncTask方式从url/服务器加载图像,并将加载的图像缓存到设备文件或缓存区域。
activity_main.xml
MainActivity.java
Try this way,hope this will help you to solve your problem.
Here I explain about how to use "AndroidQuery" external library for load image from url/server in asyncTask manner with also cache loaded image to device file or cache area.
activity_main.xml
MainActivity.java
Android Query 可以为您处理这个问题以及更多(例如缓存和加载进度)。
请查看此处。
我认为是最好的方法。
Android Query can handle that for you and much more (like cache and loading progress).
Take a look at here.
I think is the best approach.
这段代码经过测试,完全可以工作。
This code is tested, it is completely working.
来自 Android 开发人员:
确保您在
AndroidManifest.xml
中设置了以下访问互联网的权限。From Android developer:
Make sure you have the following permissions set in your
AndroidManifest.xml
to access the internet.1. Picasso 可以轻松实现应用程序中的图像加载 — 通常只需一行代码!
使用 Gradle:
只需一行代码!
2. Glide 图像加载和缓存库适用于 Android,专注于平滑滚动
使用 Gradle:
3. fresco 是一个用于在 Android 应用程序上显示图像的强大系统。 Fresco 负责图像加载和显示,因此您无需这样做。
Fresco 入门
4. < a href="https://coil-kt.github.io/coil/" rel="noreferrer">Coil 是一个由 Kotlin 协程支持的 Android 图像加载库。
要将图像加载到
ImageView
中,请使用加载扩展函数:1. Picasso allows for hassle-free image loading in your application—often in one line of code!
Use Gradle:
Just one line of code!
2. Glide An image loading and caching library for Android focused on smooth scrolling
Use Gradle:
3. fresco is a powerful system for displaying images on Android applications. Fresco takes care of image loading and display, so you don't have to.
Getting Started with Fresco
4. Coil is an image loading library for Android backed by Kotlin Coroutines.
To load an image into an
ImageView
, use the load extension function:您必须首先下载图像
然后使用 Imageview.setImageBitmap 将位图设置到 ImageView
You'll have to download the image firstly
Then use the Imageview.setImageBitmap to set bitmap into the ImageView
不管怎样,人们要求我发表评论作为答案。我正在发帖。
Anyway people ask my comment to post it as answer. i am posting.
我写了一个类来处理这个问题,因为这似乎是我的各个项目中反复出现的需求:
https://github.com/koush/UrlImageViewHelper
I wrote a class to handle this, as it seems to be a recurring need in my various projects:
https://github.com/koush/UrlImageViewHelper
如果您根据按钮单击加载图像,上面接受的答案非常好,但是如果您在新活动中执行此操作,它会使 UI 冻结一两秒钟。环顾四周,我发现一个简单的异步任务消除了这个问题。
要使用 asynctask,请在活动末尾添加此类:
并使用 onCreate() 方法进行调用:
结果是快速加载的活动和根据用户的网络速度稍后显示的图像视图。
The accepted answer above is great if you are loading the image based on a button click, however if you are doing it in a new activity it freezes up the UI for a second or two. Looking around I found that a simple asynctask eliminated this problem.
To use an asynctask add this class at the end of your activity:
And call from your onCreate() method using:
The result is a quickly loaded activity and an imageview that shows up a split second later depending on the user's network speed.
您还可以使用此 LoadingImageView 视图从以下网址加载图像:
http:// blog.blundellapps.com/imageview-with-loading-spinner/
从该链接添加类文件后,您可以实例化 url 图像视图:
在 xml 中:
在代码中:
并使用以下命令更新它:
You could also use this LoadingImageView view to load an image from a url:
http://blog.blundellapps.com/imageview-with-loading-spinner/
Once you have added the class file from that link you can instantiate a url image view:
in xml:
In code:
And update it using:
在我看来,完成此类任务的最佳现代库是 Square 的 Picasso。它允许使用单行代码通过 URL 将图像加载到 ImageView:
The best modern library for such a task in my opinion is Picasso by Square. It allows to load an image to an ImageView by URL with one-liner:
嗨,我有最简单的代码,试试这个
main.xml
试试这个
Hi I have the most easiest code try this
main.xml
try this