0

My app takes a photo using the camera. It then gets the URI, then the path of that photo and stores the path in a SQLite database. All of that seems to work fine, however when I go to view the images as bitmaps, this code only shows the same image (the last one) for every record (all others informations on each record displays correctly).

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
mPhoto = BitmapFactory.decodeFile(timage.toString(), options);
viewHolder.img_image = (ImageView) convertView.findViewById(R.id.image);
viewHolder.img_image.setImageBitmap(mPhoto);

This is where I get the information for timage, and the rest of the record details :

public ArrayList<ProductModel> _productlist = new ArrayList<ProductModel>();
ArrayList<ProductModel> product_list = db.getProducts();

for (int i = 0; i < product_list.size(); i++) {
    String tidno = product_list.get(i).getIdno();

    System.out.println("tidno>>>>>" + tidno);
    timage = product_list.get(i).getImage();
    System.out.println(timage);
    String tname = product_list.get(i).getProductname();
    String tdesc = product_list.get(i).getproductdesc();

    ProductModel _ProductModel = new ProductModel();

    _ProductModel.setIdno(tidno);
    _ProductModel.setImage(timage);
    _ProductModel.setProductname(tname);
    _ProductModel.setproductdesc(tdesc);

    _productlist.add(_ProductModel);
}

totalrecords.setText("Total Records :-" + _productlist.size());
listview.setAdapter(new ListAdapter(this));
db.close();

The first 3 lines are based on Vino's answer from Suggestions to avoid bitmap Out of Memory error - scaling the images to get rid of the out of memory errors I was receiving otherwise. mPhoto is simply a Bitmap variable, timage is the images paths (which prints onto the console correctly).

Anyone able to give me a hint in the right direction to get the app showing every image, rather than just the last one taken ?

Community
  • 1
  • 1
  • What is timage and where does it come from? – Merlevede Mar 04 '14 at 15:27
  • @Merlevede timage is a string. I have an array list called productlist, and timage is used to get the paths of the images for that list, it is entered in to the list along with a product id, name and description (all of which display correctly for each record) I've posted the code snippet above – user3027078 Mar 04 '14 at 15:40

1 Answers1

0

That variable timage is conserving the value of the last item in the for iteration.

for (int i = 0; i < product_list.size(); i++)
{
    //...
    timage = product_list.get(i).getImage();
    //...
}

So after exiting the loop timage will have the value of the last item. And of course here

mPhoto = BitmapFactory.decodeFile(timage.toString(), options);

it's being used for all your images. Shouldn't you be using _ProductModel.getImage() instead?

Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • Thank you. I think you're answer is right in terms of whats wrong, but unfortunately _ProductModel.getImage() just seems to produce the same problem. – user3027078 Mar 04 '14 at 16:23