简体   繁体   中英

How to save unique picture per ListItem Android

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. eg- 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 answer is in the question : delete the file when you delete the item. File.delete() is the method you're probably searching for. You'll also have to rename the other files if the item is not the last one in the list, though.

Another, easier way would be to use a simple counter to name your files, and associate each item in the list with the file name with the help of a Map .

I don't want to step on JB Nizet's answer but I will give another suggestion. Using SimpleCursorAdapter and a SqliteDatabase you could avoid having to manage any kind of numeric key. Just hold the path to the file in the database along with any other related information for the user, setup your ViewBinder and you're off to the races. A binder is a very flexible way to render data and even conditionally render View objects.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM