延迟加载列表 - 需要一点帮助

时间:2011-08-11 13:41:36

标签: android image android-assets lazylist

所以基本上每个人都需要这样做,将一些图像从web或缓存加载到listview。我找到了Fedor的一个非常好的懒惰列表示例,我试图让它做我需要的,但我有一些问题。我的情况下的图像是加密的。所以我需要在设备上加密它们并在列表视图中显示它们。现在我得到了这段代码:

private Bitmap getBitmap(String src) {
    Bitmap myBitmap = null;
        try {

            //Decryption
            try {
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

            AssetManager is = this.getAssets();        
            InputStream input = is.open(src); //open file in asset manager
            CipherInputStream cis = new CipherInputStream(input, cipher);

            myBitmap = BitmapFactory.decodeStream(cis);

            }
            catch(Exception e){
                e.printStackTrace();
                Log.v("ERROR","Error : "+e);
            }


            return myBitmap;


        } catch (IOException e) {
            e.printStackTrace();
            Log.v("ERROR","Error : "+e);

            return null;
        }
    }

据我所知,这不正确(我无法理解为什么,这就是我需要帮助的原因)。这是我得到的例外情况:

08-11 13:38:51.163: WARN/System.err(4731): java.lang.NullPointerException
08-11 13:38:51.163: WARN/System.err(4731):     at android.content.ContextWrapper.getAssets(ContextWrapper.java:74)
08-11 13:38:51.163: WARN/System.err(4731):     at com.custom.lazylist.ImageLoader.getBitmap(ImageLoader.java:79)
08-11 13:38:51.163: WARN/System.err(4731):     at com.custom.lazylist.ImageLoader.access$0(ImageLoader.java:70)
08-11 13:38:51.163: WARN/System.err(4731):     at com.custom.lazylist.ImageLoader$PhotosLoader.run(ImageLoader.java:200)

以下是ImageLoader类的完整代码:

package com.custom.lazylist;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Stack;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.fedorvlasov.lazylist.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;

public class ImageLoader extends Activity {

    //the simplest in-memory cache implementation. This should be replaced with something like SoftReference or BitmapOptions.inPurgeable(since 1.6)
    private HashMap<String, Bitmap> cache=new HashMap<String, Bitmap>();

    private File cacheDir;

    public ImageLoader(Context context){
        //Make the background thead low priority. This way it will not affect the UI performance
        photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);

        //Find the dir to save cached images
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
        else
            cacheDir=context.getCacheDir();
        if(!cacheDir.exists())
            cacheDir.mkdirs();
    }

    final int stub_id=R.drawable.stub;
    public void DisplayImage(String url, Activity activity, ImageView imageView)
    {
        if(cache.containsKey(url))
            imageView.setImageBitmap(cache.get(url));
        else
        {
            queuePhoto(url, activity, imageView);
            imageView.setImageResource(stub_id);
        }    
    }

    private void queuePhoto(String url, Activity activity, ImageView imageView)
    {
        //This ImageView may be used for other images before. So there may be some old tasks in the queue. We need to discard them. 
        photosQueue.Clean(imageView);
        PhotoToLoad p=new PhotoToLoad(url, imageView);
        synchronized(photosQueue.photosToLoad){
            photosQueue.photosToLoad.push(p);
            photosQueue.photosToLoad.notifyAll();
        }

        //start thread if it's not started yet
        if(photoLoaderThread.getState()==Thread.State.NEW)
            photoLoaderThread.start();
    }

    private Bitmap getBitmap(String src) {
        Bitmap myBitmap = null;
            //Decryption
            try {
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);

            AssetManager is = this.getAssets();        
            InputStream input = is.open(src); //open file in asset manager
            CipherInputStream cis = new CipherInputStream(input, cipher);

            myBitmap = BitmapFactory.decodeStream(cis);

            }
            catch(Exception e){
                e.printStackTrace();
                Log.v("ERROR","Error : "+e);
            }


            return myBitmap;
        }

    //Task for the queue
    private class PhotoToLoad
    {
        public String url;
        public ImageView imageView;
        public PhotoToLoad(String u, ImageView i){
            url=u; 
            imageView=i;
        }
    }

    PhotosQueue photosQueue=new PhotosQueue();

    public void stopThread()
    {
        photoLoaderThread.interrupt();
    }

    //stores list of photos to download
    class PhotosQueue
    {
        private Stack<PhotoToLoad> photosToLoad=new Stack<PhotoToLoad>();

        //removes all instances of this ImageView
        public void Clean(ImageView image)
        {
            for(int j=0 ;j<photosToLoad.size();){
                if(photosToLoad.get(j).imageView==image)
                    photosToLoad.remove(j);
                else
                    ++j;
            }
        }
    }

    class PhotosLoader extends Thread {
        public void run() {
            try {
                while(true)
                {
                    //thread waits until there are any images to load in the queue
                    if(photosQueue.photosToLoad.size()==0)
                        synchronized(photosQueue.photosToLoad){
                            photosQueue.photosToLoad.wait();
                        }
                    if(photosQueue.photosToLoad.size()!=0)
                    {
                        PhotoToLoad photoToLoad;
                        synchronized(photosQueue.photosToLoad){
                            photoToLoad=photosQueue.photosToLoad.pop();
                        }
                        Bitmap bmp=getBitmap(photoToLoad.url);
                        cache.put(photoToLoad.url, bmp);
                        Object tag=photoToLoad.imageView.getTag();
                        if(tag!=null && ((String)tag).equals(photoToLoad.url)){
                            BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad.imageView);
                            Activity a=(Activity)photoToLoad.imageView.getContext();
                            a.runOnUiThread(bd);
                        }
                    }
                    if(Thread.interrupted())
                        break;
                }
            } catch (InterruptedException e) {
                //allow thread to exit
            }
        }
    }

    PhotosLoader photoLoaderThread=new PhotosLoader();

    //Used to display bitmap in the UI thread
    class BitmapDisplayer implements Runnable
    {
        Bitmap bitmap;
        ImageView imageView;
        public BitmapDisplayer(Bitmap b, ImageView i){bitmap=b;imageView=i;}
        public void run()
        {
            if(bitmap!=null)
                imageView.setImageBitmap(bitmap);
            else
                imageView.setImageResource(stub_id);
        }
    }

    public void clearCache() {
        //clear memory cache
        cache.clear();

        //clear SD cache
        File[] files=cacheDir.listFiles();
        for(File f:files)
            f.delete();
    }



}

希望有人可以帮我解决这个问题,因为我真的想了解如何使用Lazy Loading List。

P.S。实际上我意识到我在没有覆盖onCreate的情况下扩展了Activity,但这是实现此目的的唯一方法:AssetManager is = is.getAssets();。否则,如果我删除,我会向我显示错误:The method getAssets() is undefined for the type ImageLoader

1 个答案:

答案 0 :(得分:2)

有两件事是错的:

  1. 如果ImageLoader不是Activity,则不应扩展Activity(如您所承认的那样)。每当您发现自己需要在不应该​​是活动的对象中扩展Activity时,您需要上下文。通常将上下文保存为成员变量private Context mContext;就足够了,然后将遇到问题的行更改为AssetManager is = this.mContext.getAssets()
  2. 这不符合你的情况。您需要使用AsyncTask或将ImageLoader转换为服务。问题是你需要在后台线程中使用Context。但是,如果创建ImageLoader的Activity(以及后台线程)消失,当后台线程调用getAssetManager()时,mContext可能不是有效的上下文。如果您将其变为服务,那么您的服务将是有效的上下文,并为您提供对资产管理员的访问权。
  3. 创建服务的确切步骤超出了本答案的范围,但您可以从有关服务的Android文档开始,然后从那里开始:http://developer.android.com/guide/topics/fundamentals/services.html

    编辑:备份评论的代码(下方)

    ...
    
    // ***** ADDITION *****
    private AssetManager mAssetManager;
    
    public ImageLoader(Context context){
        //Make the background thead low priority. This way it will not affect the UI performance
        photoLoaderThread.setPriority(Thread.NORM_PRIORITY-1);
        // ***** ADDITION *****
        mAssetManager = context.getAssets();
    
        //Find the dir to save cached images
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
        else
            cacheDir=context.getCacheDir();
        if(!cacheDir.exists())
            cacheDir.mkdirs();
    }
    
    private Bitmap getBitmap(String src) {
        Bitmap myBitmap = null;
            //Decryption
            try {
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
            cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
    
            // ***** CHANGE *****
            InputStream input = mAssetManager.open(src); //open file in asset manager
            CipherInputStream cis = new CipherInputStream(input, cipher);
    
            myBitmap = BitmapFactory.decodeStream(cis);
    
            }
            catch(Exception e){
                e.printStackTrace();
                Log.v("ERROR","Error : "+e);
            }
    
    
            return myBitmap;
        }
    ....