【问题标题】:Storing and loading images from the cache, images will store but wont load从缓存中存储和加载图像,图像将存储但不会加载
【发布时间】:2015-08-16 11:22:53
【问题描述】:

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

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

这是我的图像加载类:

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

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

这个类也是这样调用的:

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

【问题讨论】:

    标签: android caching android-asynctask android-imageview android-bitmap


    【解决方案1】:

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

    https://github.com/square/picasso

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

    并建议您轻松使用它们...:D

    【讨论】:

    • 对毕加索的调用很好,现在只需一行即可调用它
    • Picasso.with(activity).load(thumb).into(pp);
    • @PaulLedger 请不要忘记选择这个答案作为正确答案,如果是,tnx
    • 上帝保佑毕加索!
    【解决方案2】:

    你也可以试试Glide

    优点:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-22
      • 1970-01-01
      • 2018-06-27
      • 2014-01-14
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多