Android自定义Listview图像加载没有库

时间:2015-07-14 14:19:16

标签: android listview android-asynctask image-loading

我想将SD卡中的图片加载到自定义列表视图中。我的列表视图包含图像视图和文本视图。我知道有很多库可以帮助我实现这一目标,但我想不用这些库。

问题: -

我正在从异步任务加载图像。我从getView()里面调用异步任务。 我只在最后一行得到图像。也就是说,如果我的文件夹中有3个图像,那么只有图像显示在列表视图的第3个项目中。

以下是我的代码: -

public class MyPhotoAdapter extends BaseAdapter {

    private Activity callingActivity;
    private ArrayList<String> filePaths = new ArrayList<String>();//contains file path of images
    ArrayList<Bitmap> myImages = new ArrayList<Bitmap>();
    private int imageWidth;
    int position;

    ImageView iv_photo;
    TextView tv_address;


    public MyPhotoAdapter(Activity activity,ArrayList<String> paths,int width)
    {
        this.callingActivity = activity;
        this.filePaths = paths;
        this.imageWidth = width;
    }



    @Override
    public int getCount() {
        return filePaths.size();
    }

    @Override
    public Object getItem(int i) {
        return this.filePaths.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        position = i;

        LayoutInflater inflater = (LayoutInflater) callingActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.single_photo,viewGroup, false);

        iv_photo = (ImageView) row.findViewById(R.id.photoView);
        tv_address = (TextView) row.findViewById(R.id.tv_address);

        AsyncImageLoader loader = new AsyncImageLoader();
        loader.execute(filePaths);
        return row;
    }

    class AsyncImageLoader extends AsyncTask<ArrayList<String>,Bitmap,Void>{


        ProgressDialog dialog;


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = ProgressDialog.show(callingActivity,"Loading Images","Please Wait ....");
        }

        @Override
        protected Void doInBackground(ArrayList<String>... arrays) {
            Log.d("Test","Total Images :- "+arrays[0].size());
            for(int i = 0; i< arrays[0].size(); i++)
            {
                Bitmap map = decodeFile(arrays[0].get(i).toString(), 150, 150);
                myImages.add(map);
                publishProgress(map);
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(Bitmap... bitmap) {

            iv_photo.setImageBitmap(bitmap[0]);

        }

        @Override
        protected void onPostExecute(Void aVoid) {
            //super.onPostExecute(aVoid);
            dialog.dismiss();
            /*for(int i = 0; i < myImages.size(); i ++)
            {
                iv_photo.setImageBitmap(myImages.get(i));
            }*/
        }


        public Bitmap decodeFile(String filePath, int WIDTH, int HIGHT) {
            try {

                File f = new File(filePath);

                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(f), null, o);

                final int REQUIRED_WIDTH = WIDTH;
                final int REQUIRED_HIGHT = HIGHT;
                int scale = 1;
                while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
                        && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
                    scale *= 2;

                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}

图像仅显示在最后一个列表项中。其他图像视图不显示图像。

3 个答案:

答案 0 :(得分:0)

您没有处理视图回收,当您的第一个异步任务完成时,您已经创建了3个视图,因此您只将图像应用到上一个视图。

你需要处理这种并发性,在android文档中有一个很好的例子。

http://developer.android.com/training/displaying-bitmaps/process-bitmap.html#concurrency

您不应将ImageViewTextView作为全局变量存储在适配器中,它们会不断更改。

答案 1 :(得分:0)

/*
 * Retrive photo from path
 * and return list of path
 */
public static List<String> RetriveImagePathFromSDCard(String path) {
    List<String> tFileList = new ArrayList<String>();
    File f = new File(path);
    if (f.exists()) {
        File[] files = f.listFiles();
        Arrays.sort(files);

        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            if (file.isDirectory())
                continue;
            tFileList.add(file.getPath());

        }
    }

    return tFileList;
}
projectGalleryPhotoPathList = RetriveCapturedImagePath(Constant.ProjectGallery.DIR_PROJECT_GALLERY);

projectGalleryAdapter = new PhotoAdapter(Youractivity.this, projectGalleryPhotoPathList);

在适配器类

private List<String> imgPic;
...
...

public ProjectGalleryPhotoAdapter(Context c, List<String> imagePathList) {
        context = c;
        imgPic  = imagePathList;

}


public View getView(final int position, View view, final ViewGroup parent) {
    .... 
    ....
    ....
Uri uri = Uri.fromFile(new File(imgPic.get(position)));
imageView.setImageURI(uri);

}

答案 2 :(得分:0)

您可以按照this了解有关处理并发的更多信息。

我修改了您的代码.....请查看是否有帮助

public class MyPhotoAdapter extends BaseAdapter {

    private Activity callingActivity;
    private ArrayList<String> filePaths = new ArrayList<String>();
    ArrayList<Bitmap> myImages = new ArrayList<Bitmap>();
    private int imageWidth;
    int position;

    ImageView iv_photo;
    TextView tv_address;


    public MyPhotoAdapter(Activity activity,ArrayList<String> paths,int width)
    {
        this.callingActivity = activity;
        this.filePaths = paths;
        this.imageWidth = width;
    }



    @Override
    public int getCount() {
        return filePaths.size();
    }

    @Override
    public Object getItem(int i) {
        return this.filePaths.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        position = i;

        LayoutInflater inflater = (LayoutInflater) callingActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.single_photo,viewGroup, false);

        iv_photo = (ImageView) row.findViewById(R.id.photoView);
        tv_address = (TextView) row.findViewById(R.id.tv_address);

        AsyncImageLoader loader = new AsyncImageLoader(iv_photo);
        loader.execute(filePaths);
        return row;
    }

    class AsyncImageLoader extends AsyncTask<ArrayList<String>,Bitmap,Void>{


        ProgressDialog dialog;
        private final WeakReference<ImageView> imageViewReference;

        public AsyncImageLoader(ImageView imageView)
        {
            imageViewReference = new WeakReference<ImageView>(imageView);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = ProgressDialog.show(callingActivity,"Loading Images","Please Wait ....");
        }

        @Override
        protected Void doInBackground(ArrayList<String>... arrays) {
            Log.d("Test","Total Images :- "+arrays[0].size());
            for(int i = 0; i< arrays[0].size(); i++)
            {
                Bitmap map = decodeFile(arrays[0].get(i).toString(), 150, 150);
                myImages.add(map);
                publishProgress(map);
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(Bitmap... bitmap) {

            //iv_photo.setImageBitmap(bitmap[0]);

            if (imageViewReference != null) {
                ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap[0]);
                }
            }

        }

        @Override
        protected void onPostExecute(Void aVoid) {
            //super.onPostExecute(aVoid);
            dialog.dismiss();
            /*for(int i = 0; i < myImages.size(); i ++)
            {
                iv_photo.setImageBitmap(myImages.get(i));
            }*/
        }


        public Bitmap decodeFile(String filePath, int WIDTH, int HIGHT) {
            try {

                File f = new File(filePath);

                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(f), null, o);

                final int REQUIRED_WIDTH = WIDTH;
                final int REQUIRED_HIGHT = HIGHT;
                int scale = 1;
                while (o.outWidth / scale / 2 >= REQUIRED_WIDTH
                        && o.outHeight / scale / 2 >= REQUIRED_HIGHT)
                    scale *= 2;

                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}