在listview中加载图像

时间:2012-04-10 02:13:17

标签: android listview android-asynctask

我正在尝试加载图片以显示在列表视图中的文本旁边。由于在没有asynctask的情况下滚动很糟糕,我试图实现它。不幸的是,我的代码在onPostExecute()中给了我空指针错误,我似乎无法找出原因。我的代码如下:

class LoadImage extends AsyncTask<Object, Void, Bitmap>{

    private ImageView imv;
    private Object imageViewHolder;
    private String position;
    private Object path;

    public LoadImage(ImageView imv, String _position) {
        Log.w("MyApp", "creating LoadImage");
        this.imv = imv;
        Log.w("MyApp", "got image view");
        path = imv.getTag();
        Log.w("MyApp", "Got Imageview Path");
        position = _position;
        Log.w("MyApp", "LoadImage created");
    }

    @Override
    protected Bitmap doInBackground(Object... params) {
        Log.w("MyApp", "in doInBackground");
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        Log.w("MyApp", "made bitmap factory");

        Bitmap bm = null;

        Log.w("MyApp", "Getting URL ID");
        File dir = new File(PATH + position);
        Log.w("MyApp", "Have URL ID");

        if(dir.isDirectory()){
            Log.w("MyApp","Dir is a directory");

            File header = new File(dir.getAbsolutePath() + "/title");
            Log.w("MyApp", "Checking for title");
            if(header.isFile()){
                bm = BitmapFactory.decodeFile(header.getAbsolutePath(), options);
            }

            if(bm!=null){
                Log.w("MyApp", "Title is good");
            }else{
                Context c = ReadingList.this;
                bm = BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_menu_gallery);
            }
        }else{
            Log.w("MyApp", "Dir Not Directory");
            Context c = ReadingList.this;
            bm = BitmapFactory.decodeResource(c.getResources(), R.drawable.ic_menu_gallery);
        }


        return bm;
    }
    @Override
    protected void onPostExecute(Bitmap result) {
        if (!imv.getTag().equals(path)) {
            Log.w("MyApp", "Not setting images");
            return;
        }

        if(result != null && imv != null){
            Log.w("MyApp", "setting bitmap");
            imv.setImageBitmap(result);
        }else{
            Log.w("MyApp", "Nothing happened");
        }
    }

}

我正在努力解决的问题是不要让位图与循环使用的视图一起回收。我看过这个网站上的一些例子,我不确定为什么这不起作用。许多代码都是从对此question的响应中借用的。 onPostExecute不会在日志中输入任何内容,也不会立即崩溃。崩溃似乎需要大约两分钟。这些图像是从SD卡中提取出来的,所以我不确定为什么要花这么长时间来判断它是否要崩溃。

in doInBackground
made bitmap factory
Getting URL ID
Have URL ID
Dir is a directory
Checking for title
Title is good
in doInBackground
made bitmap factory
getting URL ID
Have URL ID
Dir is a directory
Checking title
Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.theholodev.glowreader.ReadingList$LoadImage.onPostExecute(ReadingList.java:305)
at com.theholodev.glowreader.ReadingList$LoadImage.onPostExecute(ReadingList.java:1)

在此之后,它继续多次重复前7行,但应用程序已崩溃,我无法看到他们是否做了什么。似乎没有再次调用onPostExecute。

供参考:

304: if (!imv.getTag().equals(path)) {
305:    Log.w("MyApp", "Not setting images");
306:    return;
307: }

1 个答案:

答案 0 :(得分:1)

我找到了一个适合我的解决方案。我使用了aQuery,并使用了他们提供的文件中的shouldDelay和async set图像。

if(header.isFile() && !aq.shouldDelay(position, convertView, parent, 
   header.getAbsolutePath())){
     bm = BitmapFactory.decodeFile(header.getAbsolutePath(), options);
     showcase.setImageBitmap(bm);
 } else {                    
     showcase.setImageResource(R.drawable.missing_image);
 }