0

I have a few images that I have stored inside the internal storage. I managed to retrieve the image file location and decode it. But I could not manage to get it to display inside a gridview. And I'm not sure what's wrong with the codes due to there is no error at the moment. Any comments will be appreciated.

    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);


}

Below is the ImageAdapter code.

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;
}  

UPDATE

    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;
}   
IssacZH.
  • 1,457
  • 3
  • 24
  • 54
  • are you getting the same image for all the grid items? Is that your issue? – G_S Dec 10 '12 at 08:06
  • nope.. sorry that I didn't mention what I get.. It did not display anything in the screen.. – IssacZH. Dec 10 '12 at 08:08
  • what is the value of variable count ? – G_S Dec 10 '12 at 08:12
  • it's 0. Does it means that it didn't read my Images? – IssacZH. Dec 10 '12 at 08:22
  • means you are creating an array of size 0. So you dont have any images in your array . Check out how many images you want to get and pass it to create an array of that size – G_S Dec 10 '12 at 08:31
  • Then I believed there is something wrong at my for loop that area. I cant really get the values in bitmap[i] and bImage. But somehow it did run the Log.i with values in it. Weird. – IssacZH. Dec 10 '12 at 08:45
  • instead of this.count while creating the bitmap array try values.length() and check it – G_S Dec 10 '12 at 08:50
  • Yea. You were right about the "this.count". I think that one of the issue. But still I cant get any image display in gridview. – IssacZH. Dec 10 '12 at 09:16
  • decodeFile() might be the culprit. You are not changing the bitmap array there. Once check it – G_S Dec 10 '12 at 09:59
  • I updated the decodeFile(). Nothing have changed. There is an issue when I try to check the bitmap[] after the loop using Log.i, it just return as null. I got no idea what happened. – IssacZH. Dec 10 '12 at 10:13

2 Answers2

1

Have a glance at this

decodeFile(String filePath) returns a bitmap that is not modified in it. So i think the bitmap is not changing at all (just returning null).

  1. You are passing a file path and returning a bitmap array . you get a single bitmap image and passing the same as a bitmap array.

2.Use bitmap[i] = decodeFile(bImage); instead of bitmap = decodeFile(bImage); and

3.change return type of decodeFile(String filePath) to simply bitmap

4.use return BitmapFactory.decodeFile(filePath,o); instead of the bitmap .

G_S
  • 7,068
  • 2
  • 21
  • 51
  • Sorry for the late reply.. I solved the unable to display image part by removing the BitmapFactory.options. But still I got another issue where there is 3 empty slot in my gridview then followed by my images. No idea what's wrong – IssacZH. Dec 11 '12 at 01:06
  • I updated it. The issue now is that the images that display out in gridview is very long. As in the height of the image. The image display out is not standard. The height and width all different. – IssacZH. Dec 11 '12 at 03:52
  • Am not sure if we can find any better solution but what i do is set the height and width of the image in the getview method (This can also be done by considering the screen devices height and width). Or alternatively you can fix the android:numColumns and android:columnWidth attributes for the gridview – G_S Dec 11 '12 at 04:06
0

I think what you want to return from getView is the holder and not the converted view

user1787773
  • 888
  • 1
  • 11
  • 19