• android删除文件出错


    当删除一个文件,再又一次下载这个同名文件,保存到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;
        }

    
  • 相关阅读:
    Response生成注册验证码实现例子02
    Mysql 自增字段起始值auto_increment的修改方法
    elite核心库的加载方式及自动加载类库
    elite核心类库之事件类
    wamp速度缓慢的解决办法
    Dwzdialog中批量提交的问题处理
    PHP中缀表达式与逆波兰式的计算(用于工资项目等四则计算)
    PHP工资计算之逆波兰式
    elite核心类库之模板类
    PHP soap访问接口出错汇总及解决办法
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5081452.html
Copyright © 2020-2023  润新知