Search code examples
androidandroid-galleryandroid-imageandroid-file

delete image from gallery with OnItemLongClick event


I want to know how can I use OnItemLongClickListener event to delete an image from gallery?

I don't know how can I find the url or another detail about the image from this event in order to delete it.

this is What I did so far (* I already have a gallery with images inside) : First I connect the gallery to the event :

gallery.setOnItemLongClickListener(OnLongClickGallery);

Then I want to ask the user if he sure that he want to delete the image and in the same time save the data from the selected item:

private OnItemLongClickListener OnLongClickGallery = new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        //How I get the desired data here?
        showPopupMenu(arg1);// Show pop up list 
        return false;
    }
};

Thanks for help.


Solution

  • I think you can take help from

    • How to get a image and implementing click listener

    http://androidsamples.blogspot.in/2009/06/how-to-display-thumbnails-of-images.html

    • How to delete a image from content provider

    Deleting a gallery image after camera intent photo taken

    ContentResolver cr = getContentResolver();
                         cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                            BaseColumns._ID + "=" + c.getString(3), null);
    

    EDIT: To get path from content provider

    image_path_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
    path[i] = cursor.getString(image_path_index);