android文件系统bug

当删除一个文件,再重新下载这个同名文件,保存到sdcard时出现error:

Caused by: libcore.io.ErrnoException: open failed: EBUSY (Device or 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,建议删除文件前先将该文件进行重命名:

    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;
    }

猜你喜欢

转载自blog.csdn.net/bangelua/article/details/45980249