Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
File file = new File(path);

if(file.exists()) {
File file2 = new File(file.getAbsolutePath());
    file2.delete();
    Toast.makeText(this, "File deleted.", Toast.LENGTH_SHORT).show();
    finish();   
}else 
{
    Toast.makeText(this, "File not exists", Toast.LENGTH_SHORT).show();        
}


What I have tried:

I am trying to delete to file but it is not deleting. The program end after exceuting finish() function showing toast "File deleted". The debugger shows the file path also but file is not deleting. It is still there.

Please help.
Posted
Updated 6-Jan-23 19:29pm
v2
Comments
Richard MacCutchan 20-Aug-20 4:57am    
You should check the return value from the delete call.
Richard MacCutchan 20-Aug-20 5:07am    
David Crow 20-Aug-20 8:30am    
Why are you calling finish()?
Vivek Kansal 27-Aug-20 23:34pm    
so that actvity or app will close after deleting the file or it will not show blank screen.
David Crow 28-Aug-20 8:44am    
So your app's sole purpose is just to delete a file?

Seems you just create a variable that has a method call.

Try:
Java
File file = new File(path);
file.delete();

if(file.exists()){
      file.getCanonicalFile().delete();
      if(file.exists()){
           getApplicationContext().deleteFile(file.getName());
           Toast.makeText(this, "File deleted.", Toast.LENGTH_SHORT).show();
      }
}

Reference: java - How do I delete files programmatically on Android? - Stack Overflow[^]
 
Share this answer
 
v2
Comments
Vivek Kansal 19-Aug-20 16:22pm    
no file is still not deleting
HI Vivek Kansal, Did you solve this issue
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900