open failed: EBUSY (Device or resource busy)

删除一个文件,再从新下载这个同名文件,保存到sdcard时出现error,部分手机出现java

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)android

此问题在小米3,华为系列手机出现几率较大。 
文件建立失败的缘由是,文件被删除后仍然被其余进程占用。 
进入adb shell,经过lsof命令查看占用该文件的进程。 
听说这是Android文件系统的bug,建议删除文件前先将该文件进行重命名:shell

删除文件安全方式:安全

 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);
        }
    }.net

 

    /**
     * 安全删除文件.
     * @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;
    }进程

相关文章
相关标签/搜索