Android:创建文件夹不起作用

时间:2017-06-29 17:47:06

标签: android directory

我想拍照并将其保存在外部存储设备中,但文件夹创建失败。 权限已设定。

File folder = new File(Environment.getExternalStorageDirectory() + 
File.separator + "DCIM" + File.separator + "ScannerApp");
                if(!folder.exists()){
                    folder.mkdirs();
                }

请帮助:)

2 个答案:

答案 0 :(得分:0)

使用以下代码初始化folder值:

File folder = new File(Environment.getExternalStorageDirectory(), "DCIM" + File.separator + "ScannerApp");

File folder = new File(new File(Environment.getExternalStorageDirectory(), "DCIM"), "ScannerApp");

答案 1 :(得分:-1)

替换此

File folder = new File(Environment.getExternalStorageDirectory() + 
File.separator + "DCIM" + File.separator + "ScannerApp");
                if(!folder.exists()){
                    folder.mkdirs();
                }

File folder = new File(Environment.getExternalStorageDirectory().getPath() +
                    File.separator + "DCIM" + File.separator + "ScannerApp");
            if(!folder.exists()){
                folder.mkdirs();
            }

问题是您使用的Environment.getExternalStorageDirectory()不会返回路径。使用Environment.getExternalStorageDirectory().getPath()获取路径。

希望这有帮助。