I have a app for which I need the user to see images even if there is no internet available. Initially I make a volley request to mysql database and I use the parsed data to display images using Picasso.
Picasso.with(context).load(magazineVersions.get(position).getMagazine_image_url()).resize(400, 500).into(holder.iv_magazine);
Now I am also downloading images in response to my volley request and storing them inside a folder named .images as follows:-
public ArrayList showJSON(String json) {
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
image_url = ParseJSON.img_url;
mag_version = ParseJSON.magversion;
download_path = ParseJSON.download_url;
sample_url = ParseJSON.sample_url;
for (String img : image_url) {
downloadImages(img);
}
ArrayList android_version = new ArrayList<>();
for (int i = 0; i < image_url.length; i++) {
MagazineVersion magazineVersion = new MagazineVersion();
magazineVersion.setMagazine_version_name(mag_version[i]);
magazineVersion.setMagazine_image_url(image_url[i]);
magazineVersion.setDownload_url(download_path[i]);
magazineVersion.setSample_url(sample_url[i]);
android_version.add(magazineVersion);
}
return android_version;
}
Code for downloadImages is as follows :-
public void downloadImages(String img_url) {
String fileName = img_url.substring(img_url.lastIndexOf('/') + 1, img_url.length());
File file = new File("/storage/emulated/0/.shatayushi/.images" + fileName);
if (file.exists() && !file.isDirectory()) {
Log.d("ImageExists", fileName);
}else
new DownloadImagesAsync().execute(img_url);
}
I would like to know how can I use the downloaded images instead of the one's stored on the server.Something as simple as:-
if(Images exist in .images folder)
{
//use picasso to display the images stored in the device
}else
{
//use picasso to display the images stored on the server
Picasso.with(context).load(magazineVersions.get(position).getMagazine_image_url()).resize(400, 500).into(holder.iv_magazine);
}