Glide: How can I download image from url, and save it to SDCard?

Created on 1 Jul 2015  路  7Comments  路  Source: bumptech/glide

In Glide , I am want to download image from url, and save it to SDCard. Then, I can use this image next time .How can I do this ?

question

Most helpful comment

Glide is for manipulating and displaying images. It sounds like you want to save files from the internet which is the job of networking libraries, like Volley and OkHttp. The fact that after downloading your files you may want to display them is another matter. So you have these options:

  • Drop the requirement of custom file names and rely on Glide's ExternalDiskCache to relocate the folder
  • Download files via networking library and then use load(File) to load from phone storage with .diskCacheStrategy(NONE).
  • Exploit downloadOnly to use Glide as a networking library and duplicate cache contents by copying the resulting File.
  • Write a custom DiskCache which may be really hard to get right

There are Glide facilities that let you map HTTP URL strings to File objects in cache (downloadOnly) or load Bitmap objects from URLs efficiently through an intermediate disk cache (basic reason why Glide exists). So I'm still curious on what other use case you have: _Why_ do you want custom file names?

All 7 comments

Can you please elaborate on what you mean by "use it next time"? It is important because most of the time you don't need to access it directly: we can suggest how to take advange fo Glide if we know the use case.

When you do a Glide.with(...).load(...).into(...); the image is automatically cached and if things don't change in your app (e.g. view size) the same load line will load the same cached image from the cache in the phone.

@TWiStErRob I mean that I want to download the network images, then save these to custom folder and custom filename.How can I do this ?

Glide is for manipulating and displaying images. It sounds like you want to save files from the internet which is the job of networking libraries, like Volley and OkHttp. The fact that after downloading your files you may want to display them is another matter. So you have these options:

  • Drop the requirement of custom file names and rely on Glide's ExternalDiskCache to relocate the folder
  • Download files via networking library and then use load(File) to load from phone storage with .diskCacheStrategy(NONE).
  • Exploit downloadOnly to use Glide as a networking library and duplicate cache contents by copying the resulting File.
  • Write a custom DiskCache which may be really hard to get right

There are Glide facilities that let you map HTTP URL strings to File objects in cache (downloadOnly) or load Bitmap objects from URLs efficiently through an intermediate disk cache (basic reason why Glide exists). So I'm still curious on what other use case you have: _Why_ do you want custom file names?

One other option would be to use a custom target like SimpleTarget and save
then save the bitmap in onResourceReady call back.

Let us know if you need any more help.

Hi! i uses okhttp3-integration with glide. my usecase is: in a recyclerview 1- show(if downloaded) or download a little size thumbnail and show it fast to user. 2- show(if downloaded) or download main image with showing download PROGRESS.
I used Retrofit for download images. what I have done is: 1- use glide to show thumbnail. 2- if onException() called(means thumb not downloaded) try to show main image with glide 3- again if onException() called download thumb(Retrofit). 4- then show it with glide.load(File). 5- download main image with download progress(Retrofit) 6- show it with glide.load(File).
I think reader know what will be done in other cases(e.g. there is thumb but there is no main image).
a lot boilerplate code is written for this. My question is there is better ways to do this? for example with futures of glide I don't know about. Thanks

@DHosseiny This is a two year old closed issue. Can you file a new issue if you need help? If there's some relation to this issue you can mention it in the new issue when you file it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Morteza-Rastgoo picture Morteza-Rastgoo  路  3Comments

piedpiperlol picture piedpiperlol  路  3Comments

FooBarBacon picture FooBarBacon  路  3Comments

ghost picture ghost  路  3Comments

sant527 picture sant527  路  3Comments