0

I have function that download an image or set of images from the server and display them. This part works fine but I want to store them in the cache to save users data using the app.

I have followed this to incorporate the caching process inside my existing function.

This is my imageload class:

package ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by paul on 15/08/2015.
 */
public class loadImage extends AsyncTask<String, String, Bitmap> {

    private Bitmap bitmap = null;
    private String type = "";
    private ImageView imageView = null;
    private ImageButton imageButton = null;
    public int size = 0;
    public boolean cache = true;
    private Map<ImageButton, String> imageViewBtn;
    private Map<ImageView, String> imageViewView;
    private ExecutorService executorService;
    private FileCache fileCache;
    private MemoryCache memoryCache;
    private String url = "";

    public loadImage (String type, Object object, Context context){
        memoryCache = new MemoryCache();
        if(type.equalsIgnoreCase("imagebutton")){
            imageButton = (ImageButton) object;
            imageViewBtn = Collections.synchronizedMap(new WeakHashMap<ImageButton, String>());
        } else {
            imageView = (ImageView) object;
            imageViewView = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
        }
        fileCache = new FileCache(context);
        executorService = Executors.newFixedThreadPool(5);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //add loader as BG
    }
    @Override
    protected Bitmap doInBackground(String... params) {
        //check if cache is true
        url = params[0];
        try {
            bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;

    }
    protected void onPostExecute(Bitmap image) {
        if(size != 0){
            image = Bitmap.createScaledBitmap(image, size, size, false);
        }
        if(type.equalsIgnoreCase("imagebutton")){
            imageButton.setImageBitmap(image);
            imageViewBtn.put(imageButton, url);
        } else {
            imageButton.setImageBitmap(image);
            imageViewBtn.put(imageButton, url);
        }
        memoryCache.put(url,image);
    }

    public boolean cache(String url) {
        bitmap = memoryCache.get(url);
        if(bitmap != null) {
            Log.d("CACHE", "Image loaded from cache");
            if(type.equalsIgnoreCase("imagebutton")){
                imageButton.setImageBitmap(bitmap);
            } else {
                imageButton.setImageBitmap(bitmap);
            }
            return true;
        }
        return false;
    }
}

In my Log it says that the images are being stored in the cache but when I check in my device settings the cache amount never changes. How would I get this to work.

Also the class is called like this:

//activity is the activity so ie login.this or whatever activity im currently in
ImageButton pp = (ImageButton) custom.findViewById(R.id.search_pp);
loadImage loadImage = new loadImage("imagebutton", pp, activity);
if (!loadImage.cache(thumb)) {
   loadImage.execute(thumb);
}
Chris Stillwell
  • 10,266
  • 10
  • 67
  • 77
Paul Ledger
  • 1,125
  • 4
  • 21
  • 46

2 Answers2

2

I suggest you to visit these prepared open source libraries:

https://github.com/square/picasso

https://github.com/nostra13/Android-Universal-Image-Loader

and recommend you to use them easy peasy... :D

Sina KH
  • 563
  • 4
  • 16
0

You can also try Glide.

Pros:

  • Very easy to use
  • Image caching mechanism is automatic
  • Lightweight
Aveek
  • 849
  • 1
  • 12
  • 28