open failed: ENOENT (No such file or directory)

问题出现场景

我需要复制一个文件到手机的data下,比如
/data/data/com.yuzhiyun.chemistry/picture/chapter1/1.jpg”
在创建输出流的时候,报错
open failed: ENOENT (No such file or directory)

解决办法

在访问一个文件之前,需要创建它的父目录,而且,由于这里涉及两层目录,必须一层一层的创建

String PACKAGE_PATH = "/data/data/com.yuzhiyun.chemistry/";
            File dir = new File(PACKAGE_PATH + "picture/");
            if (!dir.exists()) {
                dir.mkdir();
            }
            File dir2 = new File(PACKAGE_PATH + "picture/chapter1/");
            if (!dir2.exists()) {
                dir2.mkdir();
            }

而不是一次性创建两级PACKAGE_PATH的子目录

            File dir2 = new File(PACKAGE_PATH + "picture/chapter1/");
            if (!dir2.exists()) {
                dir2.mkdir();
            }

你可能感兴趣的:(Error)