java.io.FileNotFoundException:打开失败:EACCES(权限被拒绝)
问题描述:
File path = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture");
if (! path.exists()) {
path.mkdirs();
if (!path.exists()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HH_mm_ss", Locale.CHINA).format(new Date());
File imagePath = new File(path.getPath() + "_" + "IMG_" + timeStamp + ".jpg");
BufferedOutputStream fos;
try {
fos =new BufferedOutputStream(new FileOutputStream(imagePath));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
return imagePath;
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
return null;
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
return null;
}
fos = new BufferedOutputStream(new FileOutputStream(imagePath)); [我调试并发现此行导致了错误]
并且清单中的权限设置正确
fos =new BufferedOutputStream(new FileOutputStream(imagePath));[I debug and found this line cause the error]
And in manifest the permission set is right
答
此问题在Android Pie和更高版本中. 因此,在清单文件固定错误中添加此行.
This issue is in Android Pie and higher version. So, adding this line in manifest file fixed error.
<application
...
...
android:requestLegacyExternalStorage="true">
</application>