码迷,mamicode.com
首页 > 移动开发 > 详细

android删除文件出错

时间:2015-12-28 09:05:20      阅读:924      评论:0      收藏:0      [点我收藏+]

标签:

当删除一个文件,再又一次下载这个同名文件,保存到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,建议删除文件前先将该文件进行重命名:



删除文件安全方式:

 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删除文件出错

标签:

原文地址:http://www.cnblogs.com/bhlsheji/p/5081452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!