如何从arraylist中检索图像路径;在gridview中显示它们?

时间:2012-02-23 14:00:18

标签: android image sqlite

我把两个不同的图像路径存储在sdcard中。我想在gridview中显示这两个图像。你能帮帮我吗?主要问题是如果我使用整数类型的arraylist并给出id我可以看到图像,但如果我使用的是具有图像路径的字符串类型的arraylist而不是如何显示图像。 我也评论过哪里要改变,请帮帮我??

    public class ImageAdapter extends BaseAdapter 
    {
        private Context mContext;

         public ImageAdapter(Context c) 
        {
          mContext = c;    
         }

       public int getCount() {
        return mThumbIds.length;
     }

     public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

//here i am getting problem tell me how to use path of image in arraylist

    imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images    
ArrayList<String> mThumbIds=new ArrayList<String>;
mThumbIds.add("/mnt/sdcard/a.jpg");
mThumbIds.add("/mnt/sdcard/b.jpg");

1 个答案:

答案 0 :(得分:1)

您无法直接使用setImageResource()设置图像路径。而是尝试:

Bitmap myBitmap = BitmapFactory.decodeFile(mThumbIds[position]);
imageview.setImageBitmap(myBitmap);