为什么使用 Android Picasso 库下载图片?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22330772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why use Android Picasso library to download images?
提问by user3376321
Why should I download the images via the Picasso library instead of just using this code:
为什么我应该通过 Picasso 库下载图像而不是仅使用以下代码:
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try
{
in = OpenHttpGETConnection(URL);
bitmap = BitmapFactory.decodeStream(in); in.close();
}
catch (Exception e)
{
Log.d("DownloadImage", e.getLocalizedMessage());
}
return bitmap;
}
Another question:
另一个问题:
Does Picasso download the image in the UI or by background thread?
Picasso 是在 UI 中还是通过后台线程下载图像?
回答by Fattie
Just for the record for anyone new to Android or perhaps moving to Android from iOS ..........
仅供 Android 新手或从 iOS 迁移到 Android 的任何人的记录.........
Until something drastically changes, you absolutely have to use Picasso. Not a joke.
在发生重大变化之前,您绝对必须使用毕加索。不是开玩笑。
Honestly, it's that simple. The advantages are unbelievable.
老实说,就是这么简单。优点是难以置信的。
It's this easy to use:
这很容易使用:
Picasso.
with(State.mainContext).
load(parseImageFile.getUrl()).
into(null);
You very simply:
你很简单:
must do caching, and threading, with image handling on Android.
必须在 Android 上使用图像处理进行缓存和线程处理。
It's that simple. Unless you want to write that from scratch, you simply must use Picasso.
就这么简单。除非您想从头开始编写,否则您必须使用毕加索。
Note that ParseImageFileessentially doesn't work - it is utterly useless about caching and so on. There are admirable alternatives to Picasso (such as Universal Image Loader, check it out), but none work as well as Picasso, for now 2014.
请注意,ParseImageFile基本上不起作用 - 它对缓存等完全无用。Picasso 有令人钦佩的替代品(例如 Universal Image Loader,请查看),但目前 2014 年没有一款能像 Picasso 一样有效。
Noteif you move to super-advanced-stuffs... The only thing betterthan Picasso, is to make the move to Volley. but that is a huge leap.
请注意,如果您转向超级高级的东西......唯一比毕加索更好的事情就是转向排球。但这是一个巨大的飞跃。
Note that ListView scrolling on android is much, much more problematic than table handling scrolling on iOS. You could say, Android + Picasso is MORE LIKE the situation on iOS, where all the work is already done for scrolling large table views with images.
请注意,Android 上的 ListView 滚动比 iOS 上的表格处理滚动问题要多得多。你可以说,Android + Picasso 更像是在 iOS 上的情况,在那里所有的工作都已经完成了滚动带有图像的大表视图。
For today, Picasso is - simply - a central part of any Android app whatsoever. Thank goodness, it is one line of code - no setup, nothing.
就今天而言,毕加索是任何 Android 应用程序的核心部分。谢天谢地,这是一行代码 - 没有设置,什么都没有。
Again, the only thing "better than" Picasso is if you move to Volley.
再说一次,唯一“比”毕加索更好的是如果你转向 Volley。
BTW here's an excellent long article on Volley v. Picasso, if you need that...
顺便说一句,这里有一篇关于 Volley v. Picasso 的优秀长文,如果你需要的话……
http://www.bignerdranch.com/blog/solving-the-android-image-loading-problem-volley-vs-picasso/
http://www.bignerdranch.com/blog/solving-the-android-image-loading-problem-volley-vs-picasso/
回答by Sarpe
Picasso download the image in another thread and it manages for you:
Picasso 在另一个线程中下载图像,它会为您管理:
- the placeholder in the meantime the image is still downloading
- resizing
- cropping/centering/scaling
- caching ( you don't have to download the image every time)
- it even does "image fade in", which is popular/normal now
- 占位符同时图像仍在下载
- 调整大小
- 裁剪/居中/缩放
- 缓存(您不必每次都下载图像)
- 它甚至会“图像淡入”,这现在很流行/正常
It's extremely simple, here is an example:
非常简单,举个例子:
Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder)
.resize(imgWidth, imgHeight)
.centerCrop()
.into(image);
回答by Kglay Kophyo
I always used Picasso Libraryfor images.
It's very useful for managing images and no worry about Memory problem.
When I' download images from server or json , I used
我总是使用毕加索图书馆的图像。
它对管理图像非常有用,不用担心内存问题。
当我从服务器或 json 下载图像时,我使用了
Picasso.with(context).load("image url").fetch();
And i store that image url to Database or somewhere.
Now we can use that image in anywhere (offline also).
我将该图像 url 存储到数据库或某处。
现在我们可以在任何地方(也可以离线)使用该图像。
Picasso.with(context).load("image url").into(ImageView);
回答by Rajat
Picasso.with(this).load("http://webneel.com/wallpaper/sites/default/files/images/04-2013/island-beach-scenery-wallpaper.jpg").placeholder(R.mipmap.ic_launcher).fit().into(imageView,
new Callback() {@
Override
public void onSuccess() {}@
Override
public void onError() {}
});
You should download the images via the Picasso library because of the following reasons:
由于以下原因,您应该通过 Picasso 库下载图像:
- You can put a placeholder in case the image takes some time to load.
fit()
- sometimes some images do not load in imageview because of the size. This method will help you to load large images.onSuccess()
- you can perform some action when an image loads successfully.onError()
- you can perform some action when there is a problem loading an image.
- 您可以放置一个占位符,以防图像需要一些时间来加载。
fit()
- 有时由于尺寸原因,某些图像无法在 imageview 中加载。此方法将帮助您加载大图像。onSuccess()
- 当图像加载成功时,您可以执行一些操作。onError()
- 当加载图像出现问题时,您可以执行一些操作。
回答by Amrah Anam
If you will use the core method of loading image from network then it would take larger amount of code. But if we will use a 3rd party library like picasso then we will achieve our goal in few lines of code.
And Picasso Library is the best and simplest I found so far.
We only need to write the following to load an image from internet using picasso.
如果您将使用从网络加载图像的核心方法,则将需要大量代码。但是如果我们使用像 picasso 这样的 3rd 方库,那么我们将在几行代码中实现我们的目标。
毕加索图书馆是迄今为止我发现的最好和最简单的图书馆。我们只需要编写以下内容即可使用 picasso 从互联网加载图像。
Picasso.with(this)
.load("YOUR IMAGE URL HERE")
.placeholder(Your Drawable Resource) //this is optional the image to display while the url image is downloading
.error(Your Drawable Resource) //this is also optional if some error has occurred in downloading the image this image would be displayed
.into(imageView);
But if we will not use picasso library or any other library we may need to do it using AsyncTask that will require more lines of code.
但是如果我们不使用 picasso 库或任何其他库,我们可能需要使用需要更多代码行的 AsyncTask 来完成。
Source: Picasso Android Tutorial
来源:毕加索安卓教程
回答by BladeCoder
You should use an image loader library like Picasso, Volley or Universal Image Loader because they do the following things that your code doesn't do:
您应该使用像 Picasso、Volley 或 Universal Image Loader 这样的图像加载器库,因为它们会执行您的代码不会执行的以下操作:
- Efficient multithreaded networking (on background threads of course)
- Combining multiple identical requests into a single network call
- Canceling pending requests, especially during ListView items recycling
- Disk and memory caching with various expiration policies
- Images downsampling to the target view size to improve performance and reduce memory usage
- Batching UI updates to improve UI responsiveness (at least for Volley and Picasso).
- 高效的多线程网络(当然在后台线程上)
- 将多个相同的请求合并为一个网络调用
- 取消挂起的请求,尤其是在 ListView 项目回收期间
- 具有各种过期策略的磁盘和内存缓存
- 图像下采样到目标视图大小以提高性能并减少内存使用
- 批量 UI 更新以提高 UI 响应能力(至少对于 Volley 和 Picasso)。
By the way, you must never perform network requests on the UI thread and since HoneyComb, Android doesn't let you do it.
顺便说一句,您绝不能在 UI 线程上执行网络请求,因为 HoneyComb,Android 不允许您这样做。
回答by A S
I know that Picasso is an awesome library for managing images in ListView and GridView, But among all options including Picasso, I use GLIDE...
我知道毕加索是一个很棒的库,用于管理 ListView 和 GridView 中的图像,但是在包括毕加索在内的所有选项中,我使用 GLIDE ...
It really manages downloading & Caching Perfectly...(i couldn't get Picasso to use cached images after download completion But Glide did it just like a piece of cake).
它真的可以完美地管理下载和缓存......(我无法让毕加索在下载完成后使用缓存的图像但是 Glide 就像小菜一碟一样)。
Here is the Github page of GLIDE :
这是 GLIDE 的 Github 页面:
https://github.com/bumptech/glide
https://github.com/bumptech/glide
Regards....
问候....
回答by Arnav Rao
Picasso automatically handles all pitfalls associated with image downloading such as caching downloaded image, look up cache, cancelling existing request, performing image transormations and decoding, resizing and cropping of images.
Picasso 会自动处理与图像下载相关的所有陷阱,例如缓存下载的图像、查找缓存、取消现有请求、执行图像转换和解码、调整图像大小和裁剪图像。
And it allows you to customize it, you can configure memory for cache, add request transformer used for adding global information to every request, add bitmap config to handle decoding images for format not covered by default bitmap config.
它允许您自定义它,您可以为缓存配置内存,添加用于向每个请求添加全局信息的请求转换器,添加位图配置以处理默认位图配置未涵盖的格式的解码图像。
It allows you to specify memory policy and network policy which makes it to use either cache or to download from the network. It allows you to add placeholder and error images to be used while downloading and in case of error in downloading respectively.
它允许您指定内存策略和网络策略,使其使用缓存或从网络下载。它允许您分别添加下载时和下载时使用的占位符和错误图像。
Picasso downloads images asynchronously on worker threads.
Picasso 在工作线程上异步下载图像。
Here is the code using latest version 2.71828 of Picasso to load an image to ImageView on worker thread.
这是使用 Picasso 的最新版本 2.71828 在工作线程上将图像加载到 ImageView 的代码。
Picasso.get().load(productImageUrl).into(imageView);
Below code resizes and crops image.
下面的代码调整和裁剪图像。
See http://www.zoftino.com/android-picasso-image-downloading-and-caching-library-tutorialfor information. Picasso.get().load(productImageUrl).resize(400,400).centerCrop().into(imageView);
有关信息,请参阅http://www.zoftino.com/android-picasso-image-downloading-and-caching-library-tutorial。Picasso.get().load(productImageUrl).resize(400,400).centerCrop().into(imageView);
回答by nikhil
Please add the following dependency in your build.gradle(Module:app)
请在您的 build.gradle(Module:app)
compile 'com.github.bumptech.glide:glide:3.6.1'