- How to load an ImageView by URL in Android? [closed]
- 23 Answers 23
- Best method to download image from url in Android
- 14 Answers 14
- How to display image from URL in Android
- 9 Answers 9
- Load image from url in notification Android
- 9 Answers 9
- How to download and save an image in Android
- 11 Answers 11
- Edit as of 30.12.2015 — The Ultimate Guide to image downloading
- Conclusion — «I have learned about the great stuff, what should I use now?»
How to load an ImageView by URL in Android? [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago .
How do you use an image referenced by URL in an ImageView ?
23 Answers 23
Make sure you have the following permissions set in your AndroidManifest.xml to access the internet.
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.
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.
I wrote a class to handle this, as it seems to be a recurring need in my various projects:
UrlImageViewHelper will fill an ImageView with an image that is found at a URL.
The sample will do a Google Image Search and load/show the results asynchronously.
UrlImageViewHelper will automatically download, save, and cache all the image urls the BitmapDrawables. Duplicate urls will not be loaded into memory twice. Bitmap memory is managed by using a weak reference hash table, so as soon as the image is no longer used by you, it will be garbage collected automatically.
Источник
Best method to download image from url in Android
I’m using below method to download single image from url
Sometimes I get an outofmemory exception.
I am unable to catch outofmemory exception. The app will close. How to prevent this?
Is there a better method for downloading images that is also faster?
14 Answers 14
Try to use this:
And for OutOfMemory issue:
I use this library, it’s really great when you have to deal with lots of images. It downloads them asynchronously, caches them etc.
As for the OOM exceptions, using this and this class drastically reduced them for me.
Add This Dependency For Android Networking Into Your Project
After Run This Code Check Your Phone Memory You Can See There A Folder — Image Check Inside This Folder , You see There a Image File with name of «image.jpeg»
Step 1: Declaring Permission in Android Manifest
First thing to do in your first Android Project is you declare required permissions in your ‘AndroidManifest.xml’ file.
For Android Download Image from URL, we need permission to access the internet to download file and read and write internal storage to save image to internal storage.
Add following lines of code at the top of tag of AndroidManifest.xml file:
Step 2: Request required permission from user
Android allows every app to run in a sandbox. If an app needs to access certain resources or information outside that sandbox, it needs to request permission from user.
From Android 6.0 onward, Google wants developers to request permission from user from within the app, for more details on permissions read this.
Therefore for Android Download Image from URL, you’ll need to request Read Storage and Write
For this, we will use the following lines of code to first check if the required permission is already granted by the user, if not then we will request permission for storage read and write permission.
We’re creating a method ‘Downlaod Image’, you can simple call this wherever you need to download the image.
Now that we have requested and been granted the user permission, to start with android download image from url, we will create an AsyncTask, as you are not allowed to run a background process in the main thread.
In the above give lines of code, a URL and Bitmap is created, using BitmapFactory.decodeStream, file is downloaded.
The File path is created to save the image (We have created a folder named ‘AndroidDvlpr’ in DIRECTORY_PICTURES) and download is initialized.
After downloading the file MediaScannerConnection, is called to read metadata from the file and add the file to the media content provider so the image is available for the user.
In the above lines of code, we have also created a method, showToast() to show Toast. complete code here:
Источник
How to display image from URL in Android
I use this code to display image from url it work but the image is stored in device this is the problem because I want to display this image only if the user is connected to internet
How do I display image from URL in Android?
9 Answers 9
Use this library to load images.
Try this solution..
it will help you.
You can use Picasso library to load image into imageView. It is easy to use.
Add dependency in your app level build.gradle file
Load image from a url like this
Glide library is better than Picasso I’m suggesting you to use Glide
for doing this in Build.gradle file in dependency section add this code:
then in your Activity that you want to show Image use this code in your onCreate method:
Try this also Fix the size according to you..or see the resize with universal loader
Used the Android Universal Image-Loader
no_image here a drawable image without any image load in Cache
Add gradle dependencies also. download the latest jar of universal loader
You Can Use Piccaso Also and Glide also
Источник
Load image from url in notification Android
In my android application, i want to set Notification icons dynamically which will be loaded from URL. For that, i have used setLargeIcon property of NotificationBuilder in receiver .I reffered many link and tried various solutions but couldn’t get desired output. Though i downloaded that image from url and setting that bitmap in notification, it is not being displayed. Instead it displays the setSmallIcon image as large icon. I don’t know where i am going wrong. Here i am posting my code. Please help me to solve this issue. Thank you.
9 Answers 9
Changed my code as below and its working now :
How to implement BigPicture style Notification:
Miracle has been done by .setStyle(new Notification.BigPictureStyle().bigPicture(result)) :
I have done this way with:
Generate notification by AsyncTask:
AsyncTask:
you can do this using Glide like this:
Top answer in Kotlin and with Coroutines. This method applies the bitmap to the builder instead of a direct assignment, and of course if bitmap available. It’s nice because if the url is wrong it will be caught in the try/catch.
with Kotlin & RxJava
Since image is loaded from internet, it should be done async in a background thread. Either use async task or Glide (for efficient image loading).
To load image notification, you need to use «NotificationCompat.BigPictureStyle()». This requires a bitmap (which has to be extracted from image url)
Most of the API’s and methods of Glide are now deprecated. Below is working with Glide 4.9 and upto Android 10.
Display the image notification once, the bitmap is ready.
I know a good answer has been given so let’s see if we can make it easier to understand and implement.
———————Theory————————
The problem can be abstracted in two step solution namely
1) Get image from a URL
2) Decode image and pass to notification builder
1) Get image from a URL
InputStream in = new URL(«Img URL goes here eg. http://gg.com/profile.jpg»).openStream();
2) Decode and pass to notification
Bitmap bmp = null; #create a null bmp container that will be used to hold decoded img
bmp = BitmapFactory.decodeStream(in); # save the image into container
Voila! once you build the image and save it in the variable bmp, you can call it on notification builder .setLargeIcon(bmp)
———Implementation—————
Android studio will encourage you to wrap your code with try catch so the end product will look like this.
Once you have the bmp you can call it in notification builder as
Источник
How to download and save an image in Android
How do you download and save an image from a given url in Android?
11 Answers 11
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!!
Skip to the bottom of this post, copy the BasicImageDownloader (javadoc version here) into your project, implement the OnImageLoaderListener interface and you’re done.
Note: though the BasicImageDownloader handles possible errors and will prevent your app from crashing in case anything goes wrong, it will not perform any post-processing (e.g. downsizing) on the downloaded Bitmaps .
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.
Note: I have not adjusted the permission management for SDK 23+ (Marshmallow) yet, thus the project is targeting SDK 22 (Lollipop).
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 the OnImageLoaderListener 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.
Note: in case of large images, you might need to scale them down.
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 downloaded Bitmap into an ImageView . The DownloadManager 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’s NetworkImageView , 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 a DisplayImageOptions 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 the Application 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 of ImageView 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?»
Note that the following text reflects my personal opinion and should not be taken as a postulate.
- If you only need to download/save/display some images, don’t plan to use them in a Recycler-/Grid-/ListView and don’t need a whole bunch of images to be display-ready, the BasicImageDownloader should fit your needs.
- If your app saves images (or other files) as a result of a user or an automated action and you don’t need the images to be displayed often, use the Android DownloadManager.
- In case your app does a lot of networking, transmits/receives JSON data, works with images, but those are not the main purpose of the app, go with Volley.
- Your app is image/media-focused, you’d like to apply some transformations to images and don’t want to bother with complex API: use Picasso (Note: does not provide any API to track the intermediate download status) or Universal Image Loader
- If your app is all about images, you need advanced features like displaying animated formats and you are ready to read the docs, go with Fresco.
In case you missed that, the Github link for the demo project.
Источник