How to Delete Internal Storage File in Android

How to delete internal storage file in android?

File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();

Deleting files in Internal Storage folder android

I simply did it as,

public void deleteRecursive(File fileOrDirectory) {

if (fileOrDirectory.isDirectory()) {
for (File child : fileOrDirectory.listFiles()) {
if (child.getName().equals("myfile40")) {

System.out.println("File 40 - Not deleted");
} else {
deleteRecursive(child);
}
}

fileOrDirectory.delete();

}

Deleting file from internal storage

You should use the TFile and TDirectory classes in the System.IOUtils unit.

For example:

TDirectory.Delete(<YOUR DIR PATH>);

or

TFile.Delete(<YOUR FILE PATH>);

Look in Embarcadero's documentation to get the right path of your files and folders on the various platforms:

Standard RTL Path Functions across the Supported Target Platforms

Can't delete files from internal storage folder

Fixed it.

My app was requesting the user for READ permission only, not WRITE.

Deleting any file requires WRITE permission. So i changed READ to WRITE, it worked!

 if (ContextCompat.checkSelfPermission(MainActivity.this
, Manifest.permission.**WRITE**_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
{
// ...
}

Delete file from internal storage

The getFilesDir() somehow didn't work.
Using a method, which returns the entire path and filename gave the desired result. Here is the code:

File file = new File(inputHandle.getImgPath(id));
boolean deleted = file.delete();

How to delete a file from the internal memory in Android?

It turned out that my problem is on this line:

ContextWrapper cw = new ContextWrapper(this);
File MyDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);
String path = MyDirectory.getAbsolutePath();
File fileToBeDeleted = new File(getFilesDir(), ImagesNames.get(SelectedIndex)); // here
boolean WasDeleted = fileToBeDeleted.delete();

And I should have done this instead:

ContextWrapper cw = new ContextWrapper(this);
File MyDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);
String path = MyDirectory.getAbsolutePath();
File fileToBeDeleted = new File(path + "//Image" + SelectedIndex + ".jpg"); // current image
boolean WasDeleted = fileToBeDeleted.delete();

android deleting a file from internal storage

Here is your answer :

File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();


Related Topics



Leave a reply



Submit