My issue is this (Don’t let the paragraph intimidate you, my question is quite simple I believe) :
In the MainActivity(A), I have a listView. When I click on an item in the listView, I go to a second activity(B). In activity B, I have a button that allows the user to take a picture. Now I save this picture under a filename. Since I want each listItem’s picture to be unique, I make the filename of the picture that item’s position. For example, if I click item#1 in the list, the corresponding picture’s name is "1". This all works very well except when I delete an item. Then, when I add another item at the same position, my code loads the existing picture for that item. e.g.- If I have a picture called '2' saved for an item2, it’s fine. when I delete this entry and add another item, which is the new item2, my code checks, “does the picture name '2' exist?”. If yes, then set this entry’s picture to that. Obviously this is a different entry and I don’t want this to happen. Possibly, I could delete the picture along with the item, but I have no clue how to do this.
I hope I explained my logic well. The only reason I am using the position is because that is the only unique thing about the item that I can find. By the way(not sure if this is relevant), I am passing the item position as an extra in the intent between Activity A and Activity B.
How can I solve this issue or use a workaround? Thanks!
EDIT 1:
Facepalm. By looking at comments, I realize that deleting it(as I stated earlier)^^, is the way to do this. Could someone tell me how to do this then?
This the code I am using to save the Image:
private void setImage() {
if (loadPicture(getIntent().getStringExtra("position"), bitmap) != null) {
// Toast.makeText(this, "not null", Toast.LENGTH_SHORT).show();
imageView.setImageBitmap(loadPicture(
getIntent().getStringExtra("position"), bitmap));
}
}
private void savePicture(String filename, Bitmap b, Context ctx) {
try {
// ObjectOutputStream bos;
FileOutputStream out;// = new FileOutputStream(filename);
out = ctx.openFileOutput(filename, Context.MODE_WORLD_READABLE);
// bos = new ObjectOutputStream(out);
b.compress(Bitmap.CompressFormat.JPEG, 40, out);
if (b.compress(Bitmap.CompressFormat.JPEG, 40, out) == true)
// Toast.makeText(this, "returned true",
// Toast.LENGTH_LONG).show();
// bos.flush();
// bos.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Cheers.
The way I solved this issue is by incrementing a counter every time I added the item, then saving this counter in the internal memory. This way, it was not based upon the position and would not get messed up when I deleted an item. I hope this continues to work as I make modifications to my application. Thanks to all for help.