22

I have created a file stored on internal storage from an activity. How can I delete this file from another activity?

I'm thinking I'll have to get the file's directory (which I'm not sure how to) and delete it. I tried using

context.deleteFile(); 

but it won't work because I'm trying to call it from a non-static method.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Malfunction
  • 1,324
  • 4
  • 15
  • 25

2 Answers2

45

Here is your answer :

File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();
hardartcore
  • 16,886
  • 12
  • 75
  • 101
3

You can try getting the instance pointing to the file and deleting it like in this answer or this one

Community
  • 1
  • 1
Terrance
  • 11,764
  • 4
  • 54
  • 80