java.io.FileNotFoundException: open failed: EACCES (Permission denied)

Alexander picture Alexander · Sep 11, 2015 · Viewed 12.9k times · Source

I got this error when I trying to storage a bitmap into storage

     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));[I debug and found this line cause the error]

And in manifest the permission set is right

Answer

Zeeshan Fareed picture Zeeshan Fareed · Nov 11, 2019

This issue is in Android Pie and higher version. So, adding this line in manifest file fixed error.

<application
    ...
    ...
    android:requestLegacyExternalStorage="true">
</application>