Android: How To Remove Files From External Storage?
I am implementing an application which generates and create files in External storage. How can I remove that? Edit I added following code and still I am getting the same problem.
Solution 1:
Filefile=newFile(selectedFilePath);
booleandeleted= file.delete();
where selectedFilePath is the path of the file you want to delete - for example:
/sdcard/YourCustomDirectory/ExampleFile.mp3
Solution 2:
This is another way just pass your directory name to delete content of it
ex "/sdcard/abc/yourdata"
publicvoiddeleteFiles(String path) {
Filefile=newFile(path);
if (file.exists()) {
StringdeleteCmd="rm -r " + path;
Runtimeruntime= Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
} catch (IOException e) {
}
}
}
Solution 3:
I think you forgot to put the write permission in your AndroidManifest.xml file, that's why delete() always return false.
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />Solution 4:
Try the following code to delete the automatically created files and when you don't know where it is stored:
publicvoidclearApplicationData() {
File cache = getCacheDir();
File appDir = newFile(cache.getParent());
if (appDir.exists()) {
String[] children = appDir.list();
for (String s : children) {
if (!s.equals("lib")) {
deleteDir(newFile(appDir, s));
Log.i("TAG",
"**************** File /data/data/APP_PACKAGE/" + s
+ " DELETED *******************");
}
}
}
}
publicstaticbooleandeleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(newFile(dir, children[i]));
if (!success) {
returnfalse;
}
}
}
return dir.delete();
}
Solution 5:
Its very simple:
File file = new File(YOUR_IMAGE_PATH).delete();
if(file.exists())
file.delete();
Hope it will help you.
Post a Comment for "Android: How To Remove Files From External Storage?"