1

I'm creating a program that can select image from gallery and after selected, the text next to the image will change to "Delete" for delete the image. I have worked on select image from gallery, and now I don't know how to make the delete function. Can someone please help! And here is my code. Where I should put the delete.

addPhotoText.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectNewsFeedImage();
        }
    });



.....else if (items[item].equals("Choose from Gallery")) {
                Intent i = new Intent();
                i.setType("image/*");
                i.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(i, "Select File"), RESULT_LOAD_IMAGE);
            }




 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == RESULT_LOAD_IMAGE && data != null) {
            Bitmap bm = null;
            try {
                bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
            } catch (IOException e) {
                e.printStackTrace();
            }
            addPhotoIcon.setImageBitmap(bm);......
Calvin
  • 13
  • 1
  • 6
  • Are you asking how to delete the image chosen or to delete the file/image from the disk/phone? If you could post the code where you render the images or image :) – yUdoDis Jul 06 '17 at 07:17

2 Answers2

2

You need to get the path of the image that you have selected from the gallery first, next you need to delete that image from the sdcard using its path.

To get the path of the selected image refer this Get filepath and filename of selected gallery image in Android

To delete the image using the path from sdcard Android Delete Image from SD Card with OnClick

Kumar
  • 127
  • 1
  • 4
0

you should have to use URI here it is in the OnActivityResult

 Uri selectedI = data.getData();

 File file = new File(String.valueOf(selectedI));
 file.delete();
jigar savaliya
  • 474
  • 1
  • 8
  • 21