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
Errors Managing the Unityplayer Lifecycle in a Native Android Application
Android Cardview Remove Padding
Push Up Content When Clicking in Edit Text
Change Date String Format in Android
Install App via Usb: the Device Is Temporarily Restricted
Set Visibility in Menu Programmatically Android
How to Check If Running on UI Thread in Android
Android Emulator - Trouble Creating User Accounts
Proguard Warnings "Can't Write Resource [Meta-Inf/Manifest.Mf] (Duplicate Zip Entry)"
Tablayout Tab Title Text in Lower Case
Android - Loop Part of the Code Every 5 Seconds
How to Launch the 'Add Contact' Activity in Android
How to Implement Rounded Corners to a Mapfragment
How to Add Parameters to API (Http Post) Using Okhttp Library in Android
Mediaplayer Setdatasource, Better to Use Path or Filedescriptor