从缓存中存储和加载图像,图像将存储但不会加载

Storing and loading images from the cache, images will store but wont load

我有从服务器下载一个或一组图像并显示它们的功能。这部分工作正常,但我想将它们存储在缓存中以使用应用程序保存用户数据。

我已按照 this 将缓存过程合并到我现有的函数中。

这是我的图片加载 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;
    }
}

在我的日志中它说图像存储在缓存中,但是当我检查我的设备设置时缓存量永远不会改变。我该如何让它工作。

另外 class 是这样调用的:

//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);
}

我建议您访问这些准备好的开源库:

https://github.com/square/picasso

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

并推荐您轻松使用它们...:D

您也可以尝试Glide

优点:

  • 非常好用
  • 图像缓存机制是自动的
  • 轻巧