无法在gridview中显示图像(内部存储)

时间:2012-12-10 07:55:27

标签: android gridview android-gridview

我有一些图像存储在内部存储器中。我设法检索图像文件位置并解码它。但我无法让它在gridview中显示。由于目前没有错误,我不确定代码有什么问题。任何意见将不胜感激。

    protected void onCreate(Bundle savedInstanceState) 
{   
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web_tab);
    helper = new DBHelper(this);
    Object[] values = helper.get_contentByEmailID(EMAIL);
    this.arrPath = new String[this.count];
    this.thumbnailsselection = new boolean[this.count];

    Log.i(TAG, "values:" +values);
    Log.i(TAG, "filepath:" +values[0]);
    Log.i(TAG, "filepath:" +values[1]);
    Log.i(TAG, "values:" +values.length);

    if(values.length>0){
        for (int i=0;i<values.length;i++){
            Log.i(TAG, "values[]" +values[i]);
            String bImage = (String) values[i];
            bitmap = new Bitmap [this.count];
            bitmap = decodeFile(bImage);


            Log.i(TAG, "bImage"+i+":" +bImage);
            Log.i(TAG, "bitmap"+i+":" +bitmap);
        }
    }
    else{
        Log.i(TAG, "Unable to locate images");
    }


    imagegrid = (GridView) findViewById(R.id.WebImageGrid);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);


}

以下是ImageAdapter代码。

public class ImageAdapter extends BaseAdapter
{
    private Context mContext;       
    Bitmap[] mImageArray;
    private LayoutInflater mInflater;

    public ImageAdapter() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return count;
    }
    public Object getItem(int position)
    {
          return position;
    }
    public long getItemId(int position)
    {
          return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {  
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.galleryitem, null);
            holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
            holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);

            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checkbox.setId(position);
        holder.imageview.setId(position);


        holder.imageview.setImageBitmap(bitmap[position]);
        holder.checkbox.setChecked(thumbnailsselection[position]);
        holder.id = position;
        return convertView;
    }
}
class ViewHolder {
    ImageView imageview;
    CheckBox checkbox;
    int id;
}               

public Bitmap[] decodeFile(String filePath) 
{
    System.out.println("filepath in decode file .. "+filePath);
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    return bitmap;
}  

更新

    Object[] values = helper.get_wbm_synccontentByEmailID(SettingConstant.EMAIL);
    count=values.length;
    this.arrPath = new String[count];
    this.thumbnailsselection = new boolean[count];
    Log.i(TAG, "values:" +values.length);
    String bImage;
    if(count>0){
        bitmap = new Bitmap [count];

        for (int i=0;i<count;i++){
            Log.i(TAG, "values[]" +values[i]);
            bImage = (String) values[i];
            Bitmap newBitmap = decodeFile(bImage);
            this.arrPath[i] = bImage;
            this.bitmap[i] = newBitmap;

        }



    public Bitmap decodeFile(String filePath) 
{
    System.out.println("filepath in decode file .. "+filePath);
    Bitmap bitmapnew = BitmapFactory.decodeFile(filePath);

    return bitmapnew;
}   

2 个答案:

答案 0 :(得分:1)

瞥一眼

decodeFile(String filePath)返回未修改的位图。 所以我认为位图根本没有改变(只返回null)。

  1. 您正在传递文件路径并返回位图数组。你得到一个位图图像并将其作为位图数组传递。
  2. 2.使用bitmap[i] = decodeFile(bImage);代替bitmap = decodeFile(bImage);

    3.将return type的{​​{1}}更改为decodeFile(String filePath)

    4.使用bitmap代替位图。

答案 1 :(得分:0)

我认为你想从getView返回的是持有者,而不是转换后的视图