When deleting a file and downloading the same name again, the error occurs when saving to sdcard, and some phones appear
caused By:libcore. io. Errnoexception: Open failed:ebusy (Deviceor resource busy)
At Libcore. Io. Posix. Open(Native Method)
At Libcore. Io. Blockguardos. Open(Blockguardos. Java:+)
At Java. io. File. createnewfile(file. java:941)
This issue is in Xiaomi 3. Huawei series mobile phones have a large probability of appearing.
The reason for file creation failure is. The file is still being consumed by other processes after it is deleted.
Enter the ADB shell. View the process that occupies the file by using the lsof command.
It is said that this is an Android file system bug, it is recommended to delete the file before you rename the file:
To remove a file security method:
private void DeleteFile (file file) {
if (File.isfile ()) {
deletefilesafely (file);
Return
}
if (File.isdirectory ()) {
file[] Childfiles = File.listfiles ();
if (Childfiles = = NULL | | childfiles.length = = 0) {
deletefilesafely (file);
Return
}
for (int i = 0; i < childfiles.length; i++) {
DeleteFile (Childfiles[i]);
}
deletefilesafely (file);
}
}
/**
* Safely delete files.
* @param file
* @return
*/
public static Boolean deletefilesafely (file file) {
if (file! = null) {
String Tmppath = file.getparent () + File.separator + system.currenttimemillis ();
File TMP = new file (Tmppath);
File.renameto (TMP);
return Tmp.delete ();
}
return false;
}
Error in Android Delete file