最近在做项目时保存文件时老是抛java.io.FileNotFoundException: /storage/emulated/0/ (Permission denied)异常 说什么权限不够,我在这里记录一下,权限什么的都打开了,动态权限什么的也申请了 就是抛这个异常。
抛异常时的写法:
1.
String sdCardPath = Environment.getExternalStorageDirectory().getPath();
// 图片文件路径
String filePath = sdCardPath + File.separator + "screenshot.png";
File file = new File(filePath);
FileOutputStream out = new FileOutputStream(file);
2.
File fileFolder = new File(mImagePath);
if (!fileFolder.exists())
fileFolder.mkdirs();
File file = new File(mImagePath, mImageName);
if (!file.exists()) {
Log.d(TAG, "file create success ");
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file);
以上两种写法都抛上述异常。
后来改成以下写法:
File file = new File(getExternalFilesDir(null), "jietu.jpg")
FileOutputStream os = new FileOutputStream( file );
就可以了,我也没去分析纠结原因,只是记录一下解决方法,以免下次又遇到相同的问题。有大佬知道原因的可以告知一下。