当前位置: 首页 > 知识库问答 >
问题:

java.io.FileNotFoundException:打开失败:EACCES(拒绝权限)

漆雕誉
2023-03-14
     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;
    }

并且在清单中权限集是正确的

共有1个答案

龙佐
2023-03-14

本期在Android派及更高版本。因此,在清单文件中添加这一行可以修复错误。

<application
    ...
    ...
    android:requestLegacyExternalStorage="true">
</application>
 类似资料: