How do you download and save an image from a given url in Android?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
OUTPUT
Make sure you added permission to write data in memory
I have just came from solving this problem on and I would like to share the complete code that can download, save to the sdcard (and hide the filename) and retrieve the images and finally it checks if the image is already there. The url comes from the database so the filename can be uniquely easily using id.
first download images
Then create a class for saving and retrieving the files
Then To access the images first check if it is already there if not then download
As Google tells, for now, don't forget to add also readable on external storage in the manifest :
Source : http://developer.android.com/training/basics/data-storage/files.html#GetWritePermission
this code perfectly run in my project
Edit as of 30.12.2015 - The Ultimate Guide to image downloading
last major update: Mar 31 2016
TL;DR a.k.a. stop talking, just give me the code!!
Since this post has received quite a lot of attention, I have decided to completely rework it to prevent the folks from using deprecated technologies, bad programming practices or just doing silly things - like looking for "hacks" to run network on the main thread or accept all SSL certs.
I've created a demo project named "Image Downloader" that demonstrates how to download (and save) an image using my own downloader implementation, the Android's built-in
DownloadManager
as well as some popular open-source libraries. You can view the complete source code or download the project on GitHub.In my conclusion at the end of this post I will share my humble opinion about the proper use-case for each particular way of image downloading I've mentioned.
Let's start with an own implementation (you can find the code at the end of the post). First of all, this is a BasicImageDownloader and that's it. All it does is connecting to the given url, reading the data and trying to decode it as a
Bitmap
, triggering theOnImageLoaderListener
interface callbacks when appropriate. The advantage of this approach - it is simple and you have a clear overview of what's going on. A good way to go if all you need is downloading/displaying and saving some images, whilst you don't care about maintaining a memory/disk cache.--
Android DownloadManager is a way to let the system handle the download for you. It's actually capable of downloading any kind of files, not just images. You may let your download happen silently and invisible to the user, or you can enable the user to see the download in the notification area. You can also register a
BroadcastReceiver
to get notified after you download is complete. The setup is pretty much straightforward, refer to the linked project for sample code.Using the
DownloadManager
is generally not a good idea if you also want to display the image, since you'd need to read and decode the saved file instead of just setting the downloadedBitmap
into anImageView
. TheDownloadManager
also does not provide any API for you app to track the download progress.--
Now the introduction of the great stuff - the libraries. They can do much more than just downloading and displaying images, including: creating and managing the memory/disk cache, resizing images, transforming them and more.
I will start with Volley, a powerful library created by Google and covered by the official documentation. While being a general-purpose networking library not specializing on images, Volley features quite a powerful API for managing images.
You will need to implement a Singleton class for managing Volley requests and you are good to go.
You might want to replace your
ImageView
with Volley'sNetworkImageView
, so the download basically becomes a one-liner:If you need more control, this is what it looks like to create an
ImageRequest
with Volley:It is worth mentioning that Volley features an excellent error handling mechanism by providing the
VolleyError
class that helps you to determine the exact cause of an error. If your app does a lot of networking and managing images isn't its main purpose, then Volley it a perfect fit for you.--
Square's Picasso is a well-known library which will do all of the image loading stuff for you. Just displaying an image using Picasso is as simple as:
By default, Picasso manages the disk/memory cache so you don't need to worry about that. For more control you can implement the
Target
interface and use it to load your image into - this will provide callbacks similar to the Volley example. Check the demo project for examples.Picasso also lets you apply transformations to the downloaded image and there are even other libraries around that extend those API. Also works very well in a
RecyclerView
/ListView
/GridView
.--
Universal Image Loader is an another very popular library serving the purpose of image management. It uses its own
ImageLoader
that (once initialized) has a global instance which can be used to download images in a single line of code:If you want to track the download progress or access the downloaded
Bitmap
:The
opts
argument in this example is aDisplayImageOptions
object. Refer to the demo project to learn more.Similar to Volley, UIL provides the
FailReason
class that enables you to check what went wrong on download failure. By default, UIL maintains a memory/disk cache if you don't explicitly tell it not to do so.Note: the author has mentioned that he is no longer maintaining the project as of Nov 27th, 2015. But since there are many contributors, we can hope that the Universal Image Loader will live on.
--
Facebook's Fresco is the newest and (IMO) the most advanced library that takes image management to a new level: from keeping
Bitmaps
off the java heap (prior to Lollipop) to supporting animated formats and progressive JPEG streaming.To learn more about ideas and techniques behind Fresco, refer to this post.
The basic usage is quite simple. Note that you'll need to call
Fresco.initialize(Context);
only once, preferable in theApplication
class. Initializing Fresco more than once may lead to unpredictable behavior and OOM errors.Fresco uses
Drawee
s to display images, you can think of them as ofImageView
s:As you can see, a lot of stuff (including transformation options) gets already defined in XML, so all you need to do to display an image is a one-liner:
Fresco provides an extended customization API, which, under circumstances, can be quite complex and requires the user to read the docs carefully (yes, sometimes you need to RTFM).
I have included examples for progressive JPEG's and animated images into the sample project.
Conclusion - "I have learned about the great stuff, what should I use now?"
Recycler-/Grid-/ListView
and don't need a whole bunch of images to be display-ready, the BasicImageDownloader should fit your needs.JSON
data, works with images, but those are not the main purpose of the app, go with Volley.In case you missed that, the Github link for the demo project.
And here's the
BasicImageDownloader.java
it might help you..