Reputation: 1248
I am trying to delete a file, but the file is not being deleted and my application is not throwing any errors. Below is my code:
final File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + '/' + "howmany.txt");
Uri uri = Uri.fromFile(file);
Boolean k = new File(uri.getPath()).delete();
if(k){
Toast.makeText(getApplicationContext(), "DELETED", Toast.LENGTH_SHORT).show();
}
I put this code right after checking for permissions, and from my understanding, .delete()
returns true if the action is completed, so if it is I want to display a toast but the toast is never displayed. The strangest thing is that I am not getting any errors, but it just isn't working.
Upvotes: 0
Views: 437
Reputation: 1892
You don't need to create a Uri of the file object and then create a file object from that Uri. Just delete the file object and get the boolean result from that. You also don't need to create the k
boolean. You can just test the delete itself:
final File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + '/' + "howmany.txt");
if (file.delete())
Toast.makeText(getApplicationContext(), "DELETED", Toast.LENGTH_SHORT).show();
Upvotes: 1