標籤:
當刪除一個檔案,再重新下載這個同名檔案,儲存到sdcard時出現error,部分手機出現
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:110)
at java.io.File.createNewFile(File.java:941)
此問題在小米3,華為系列手機出現機率較大。
檔案建立失敗的原因是,檔案被刪除後仍然被其他進程佔用。
進入adb shell,通過lsof命令查看佔用該檔案的進程。
據說這是android檔案系統的bug,建議刪除檔案前先將該檔案進行重新命名:
刪除檔案安全方式:
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);
}
}
/**
* 安全刪除檔案.
* @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;
}
android刪除檔案出錯