4

I'm trying to simply delete an image from a simple app. I have it so that when you click on the image, it'll bring up an a dialog with the option to delete it. I thought this would just be something simple, but everything I have been trying doesn't seem to be doing anything. Below is my code. Any ideas would be greatly appreciated.

    delete.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
    int id = viewIt.getId();

    File file = new File("file://" + arrPath[id]+".jpg");
    file.delete();


    }
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
BossWalrus
  • 770
  • 1
  • 12
  • 29
  • are you getting any error? please provide any errors you are getting. – jaga Apr 17 '13 at 05:49
  • you have not provided the error. make sure you have the following permission in manifest "android.permission.WRITE_EXTERNAL_STORAGE" – Rachita Nanda Apr 17 '13 at 05:52
  • No error message is received. It just doesn't do anything. I have already added the permission to the manifest too. – BossWalrus Apr 17 '13 at 06:11

1 Answers1

9

Have you added permission in manifest file ?

Any app that declares the WRITE_EXTERNAL_STORAGE permission is implicitly granted READ_EXTERNAL_STORAGE permission.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>     

Get the path of the file required and delete the file

File file= new File(android.os.Environment.getExternalStorageDirectory()+ "/myfolder/myimage.jpg");
    if(file.exists())
    {
         file.delete();
    }
duggu
  • 37,851
  • 12
  • 116
  • 113
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thanks! I wasn't getting the right file path, that is what was throwing it off. – BossWalrus Apr 17 '13 at 06:45
  • what would you recommend to dynamically delete a file? I would like to delete whatever file is clicked, not a specific one. I was trying File file = new File(Environment.getExternalStorageDirectory() + "/myfolder/" + id + ".jpg" but that didn't work – BossWalrus Apr 17 '13 at 21:51
  • if you display a image you need the path. use the same path to delete the file – Raghunandan Apr 17 '13 at 22:04