0

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);
}
Mohammadreza Khatami
  • 1,444
  • 2
  • 13
  • 27
anup
  • 585
  • 12
  • 34

1 Answers1

0

Used the Android Universal Image-Loader This is best images store in cache memory

Declare

    private ImageLoader imageLoader1;

On Create

 imageLoader1 = ImageLoader.getInstance();

 imageLoader1.init(ImageLoaderConfiguration.createDefault(getActivity()));

no_image here a drawable image without any image load in Cache

   DisplayImageOptions
            options = new DisplayImageOptions.Builder()
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .showImageOnLoading(R.drawable.no_image) // resource or drawable
            .showImageForEmptyUri(R.drawable.no_image) // resource or drawable
            .showImageOnFail(R.drawable.no_image)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();

    imageLoader1.displayImage(yourpath.replace(" ", "%20"), ivprofile, options);

You Can Use Piccaso Also for this store image in cache memory for this used also okhttp... used this...

 if (imageURL != null) {
Picasso.with(this).load(imageURL).error(R.drawable.no_Image)
.into(ivImage);
}

also compile with Okhttp ....

Arjun saini
  • 4,223
  • 3
  • 23
  • 51