How to delete internal storage file in android?
File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.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)
{
// ...
}
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();
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();
}
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();
Related Topics
Android:Change Button Text and Background Color
How to Calculate Azimuth, Pitch, Orientation When My Android Device Isn't Flat
Inflate Layout Programmatically Within Another Layout
Android.Content.Res.Resources$Notfoundexception: Unable to Find Resource Id #0Xffffffff
Android Studio: Default Project Directory
Swiperefreshlayout + Webview When Scroll Position Is at Top
Searchview in Optionsmenu Not Full Width
How to Detect "Recent Apps" System Button Clicks (Honeycomb+)
How to Draw Dashed Polyline with Android Google Map Sdk V2
How Does One Implement Drag and Drop for Android Marker
Errors Managing the Unityplayer Lifecycle in a Native Android Application
Android - Getting from a Uri to an Inputstream to a Byte Array
How to Implement Pagination in Android Listview
Start New Activity and Finish Current One in Android
Communicating with Serial Usb Device Over Android
Android/Eclipse Panic: Could Not Open
How to Fix "Fail to Connect to Camera Service" Exception in Android Emulator