I need to download several images to directory so that content can be accessed offline I need to download several images to directory so that content can be accessed offline json json

I need to download several images to directory so that content can be accessed offline


I'm using ormlite to store objects with urls too, I have a synchronization after the "sign in" screen on my app, on my experience I really recommend this library https://github.com/thest1/LazyList

It's very simple:

ImageLoader imageLoader=new ImageLoader(context);imageLoader.DisplayImage(url, imageView);

This library saves the image using the url on the external sd with basic and simple configuration about the memory issues, so if you actually have two or more items with the same url this library works perfectly, the url and imageView are the parameters, if the image is not on the phone begins a new task and put the image in the view when the download is finish, and btw this library also saves the images encoded, so these pictures don't appear on the gallery.Actually you only need these files to implement the library:https://github.com/thest1/LazyList/tree/master/src/com/fedorvlasov/lazylist

If you wanna manipulate some files, you can change the folder name in the FileCache class:

 public FileCache(Context context){    //Find the dir to save cached images    ...        cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");    ...}

Where "LazyList" is the folder name, and them you can delete, move, etc.Delete sample:

 /** * This method delete a file if exist */public static void deleteFile(File file){    if(file!=null && file.exists()) {        file.delete();    }}

Now I learned more about memory cache and the allocation of a bitmap to heap memory, for the first time manipulating images online and offline, I recommend this library, also when you learn more about it, you can implement and edit the library to your needs.


1: Use an IntentService to do your downloads.

http://developer.android.com/reference/android/app/IntentService.html

2: Set up your IntentService using AlarmManager so that it runs even if the application is not running. You register with the AlarmManager

http://developer.android.com/reference/android/app/AlarmManager.html

There are a variety of ways you can have the AlarmManager start your intent.

For Example: // Register first run and then interval for repeated cycles. alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + DEFAULT_INITIAL_RUN, DEFAULT_RUN_INTERVAL, pi);

3: Storing Data There are several options here depending on how public you want your pictures/data to be.

http://developer.android.com/reference/android/os/Environment.html

Example: External Public Storage File dirBackup = Environment.getExternalStoragePublicDirectory( "YourDirectory" );

4: Downloading

Your option here. You can using anything from your current API to a basic URLConnection.

You may want to look at:

http://developer.android.com/reference/android/app/DownloadManager.html

Also, watch your permissions you will need to add and

Hope this points you in a useful direction.


Try out this library to manage images loading.

Use a service to download the images. This I feel is mandatory because I do not know how many images there will be, and I want the download to proceed even if user exits the app

All downloading done in worker threads so it's alive while application process is alive. There may a problem appear: application dies while loading is in progress. To workaround this I suggest to use AlarmManager in combination with Service. Set it up to start by timer, check you database or UIL cache for image files being not loaded and start their loading again.

Glide has a download only option for images and you can configure where its cache is located (internal private or external public) as I read here and here. Problem is I do not feel comfortable with setting the cache size as I do not know what is required. I would like to set unlimited.

UIL has several disc cache implementations out of the box including unlimited one. It also provides you cache interface so you can implement your own.

I need to be able to delete the saved menu data especially if its saved on the external public directory as this is not removed when the app is deleted etc. or if the user chooses to delete a saved menu from within the app. I was thinking I could store the file image URIs or location of the entire saved menu in database for this but not sure if this is a good way

UIL generates unique filename for each loaded file using provided file link. You can delete any loaded image or cancel any download using link from your JSON.

I read in different sources and answers that in this use case for just caching images to SD card etc. that I should specifically use a network library to do so to avoid the allocation of a bitmap to heap memory. I am using OK HTTP in my app at the moment.

UIL does it OK. It manages memory very accurately also provide you several options for memory management configuration. For example you can choose between several memory cache implementations out of the box.

In conclusion I suggest you to the visit the link above and read library documentation/description by yourself. It's very flexible and contatins lots of useful features.