0

I use this code:

String path = "mnt/sdcard/ten-file.mp3";
File file = new File(path);
boolean result = file.delete(); 

But it doesn't delete the file. Any advice?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
xuanthucit
  • 11
  • 2
  • 2
    So hardcoded. I recommend you to use [Enviroment.getExternalStorageDirectory()](http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()) class. – Simon Dorociak Mar 27 '13 at 18:48

2 Answers2

0
String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "ten-file.mp3";  
File soundFile = new File(fileName);
if (soundFile.exists())
{
    boolean result = file.delete(); 
}    

Manifest permission

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
0

You want to make sure that the file exists prior to actually deleting the file:

File file = getBaseContext().getFileStreamPath("/sdcard/appname/data.xml");
if(file.exists()) { 
    boolean result = file.delete()
}   

The issue I think with your original code is that you didn't actually test to make sure the file existed. You just created a file variable then told it delete it. I referred to the following question from an individual who had a similar problem to you:

Android how to check if file exist and else create one?

Community
  • 1
  • 1
Dion Pezzimenti
  • 243
  • 2
  • 11