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

3

9

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

Jabalpur answered 11/9, 2015 at 12:54 Comment(9)
what all permissions you have given?Tolmach
"And in manifest the permission set is right" -- please post the manifest. Also, what version of Android are you testing on?Denten
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.mount_unmount_filesystems" /> the permission is out of <application>tagJabalpur
minSdkVersion 17 targetSdkVersion 22Jabalpur
Maybe try imagePath.createFile() before open OutputStream ?Scheider
imagePath.createNewFile() get java.io.IOException: open failed: EACCES (Permission denied) and I didn't found method createFIle()Jabalpur
ANDROID.PERMISSION should also be android.permission.Parotic
I write both <uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE" />and <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />but I get FileNotFoundException in the same lineJabalpur
I remove the code: imagePath.createFile() ; it's can working but when the fos.close(); it throws an IOException e.Jabalpur
M
19

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

<application
    ...
    ...
    android:requestLegacyExternalStorage="true">
</application>
Milkandwater answered 11/11, 2019 at 8:36 Comment(1)
This is a temporary fix or until Android 11 is out.Wellturned
V
10

on Android10

use method -> Environment.getExternalStoragePublicDirectory()

java.io.FileNotFoundException: /storage/emulated/0/Download open failed: EACCES (Permission denied)

Before your app is fully compatible with scoped storage, you can temporarily opt out based on your app's target SDK level or the requestLegacyExternalStorage manifest attribute:

Google has a new feature on Android Q: filtered view for external storage. A quick fix for that is to add this code in the AndroidManifest.xml file:


<manifest ... >
  <!-- This attribute is "false" by default on apps targeting
       Android 10 or higher. -->
  <application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
</manifest>
Visionary answered 15/3, 2020 at 13:18 Comment(0)
J
4

The problem is my permission format was wrong

Uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE

The android.permission should be lowercase

uses-permission android:name=android.permission.WRITE_EXTERNAL_STORAGE

and if I add imagePath.createNewFile();

it's also throws FileNotFoundExceptions.

Jabalpur answered 11/9, 2015 at 13:54 Comment(1)
Thanks, I had the same problems. The Uppercase permissions were auto-generated by Android StudioMarquise

© 2022 - 2024 — McMap. All rights reserved.