Open failed: EACCES (permission denied)

I have a very strange problem with accessing storage on some devices. The application works on my test devices (Nexus 4 and 7, Samsung GS5). All my devices are running Android 4.4.2. But I received many emails from users, stating that the application cannot write to the storage (neither internal storage, nor SD card). From the log file received from user reviews, I see that the problem is the following code:

try { if (fStream == null) { fStream = new FileOutputStream(filename, true); } fStream.write(data, 0, bytes); return; } catch (IOException ex) { ex.printStackTrace(); } 

It throws an exception in the line fStream = new FileOutputStream (filename, true); when creating a FileOutputStream.

Stack Log:

 W/System.err( 8147): Caused by: java.io.FileNotFoundException: /storage/emulated/0/my_folder/test_file_name.png: open failed: EACCES (Permission denied) w/System.err( 8147): at libcore.io.IoBridge.open(IoBridge.java:409) W/System.err( 8147): at java.io.FileOutputStream.<init>(FileOutputStream.java:88) W/System.err( 8147): at java.io.FileOutputStream.<init>(FileOutputStream.java:128) W/System.err( 8147): at myapp.save(SourceFile:515) W/System.err( 8147): ... 8 more W/System.err( 8147): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied) W/System.err( 8147): at libcore.io.Posix.open(Native Method) W/System.err( 8147): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) W/System.err( 8147): at libcore.io.IoBridge.open(IoBridge.java:393) W/System.err( 8147): ... 11 more 

The following permissions are declared in AndroidManifest.xml:

  <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

I confirmed that users are using the correct application on the SD card. And what is more strange is that he cannot write to the internal storage. How can this happen if I have read and write permissions? Users say that they do not connect their devices to the PC at this time.

Update

It turns out that I too often call open and close FileOutputStream, which at some point throws a FileNotFoundException. Sounds more like a thread problem.

+55
android filenotfoundexception
May 7, '14 at 20:30
source share
15 answers

I ran into a similar problem a while ago.

Your problem may be in two different areas. This is either how you create the file for recording, or your recording method may be wrong, because it depends on the phone.

If you write the file to a specific location on the SD card, try using environment variables. They should always point to the correct location. Here is an example to write to the download folder:

 java.io.File xmlFile = new java.io.File(Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/Filename.xml"); 

If you write the file to the internal storage of the application. Try this example:

 java.io.File xmlFile = new java.io.File((getActivity() .getApplicationContext().getFileStreamPath("FileName.xml") .getPath())); 

Personally, I rely on external libraries to handle streaming to a file. This one has not failed me yet.

 org.apache.commons.io.FileUtils.copyInputStreamToFile(is, file); 

I lost data too many times due to an unsuccessful write command, so I rely on well-known and proven libraries for my heavy I / O.

If the files are large, you can also see how I / O works in the background, or use callbacks.

If you are already using environment variables, this may be a permission issue. Check out Justin Fiedler's answer below.

+32
May 7 '14 at 22:09
source share
β€” -

For API 23+, you need to request read and write permissions, even if they are already in your manifest.

 // Storage Permissions private static final int REQUEST_EXTERNAL_STORAGE = 1; private static String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; /** * Checks if the app has permission to write to device storage * * If the app does not has permission then the user will be prompted to grant permissions * * @param activity */ public static void verifyStoragePermissions(Activity activity) { // Check if we have write permission int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (permission != PackageManager.PERMISSION_GRANTED) { // We don't have permission so prompt the user ActivityCompat.requestPermissions( activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE ); } } 

AndroidManifest.xml

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+123
Oct 22 '15 at 19:20
source share

Android Q- targeted apps are provided with a filtered view in external storage by default. A quick fix to this problem is to add this code to AndroidManifest.xml:

 <manifest ... > <!-- This attribute is "false" by default on apps targeting Android Q. --> <application android:requestLegacyExternalStorage="true" ... > ... </application> </manifest> 

Read more about this here: https://developer.android.com/preview/privacy/scoped-storage

+10
Aug 26 '19 at 7:52
source share

In my case, I had the wrong case in

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

android.permission must be lowercase, and the entire chain in our source is uppercase.

+6
Sep 25 '15 at 18:23
source share

I also ran into the same problem. After hard work, I discovered what was wrong with my case. My device was connected to a computer via a USB cable. There are types of USB connections such as Mass Storage, Media Device (MTP), Camera (PTP), etc. My connection type was Mass Storage, and this caused problems. When I changed the type of connection, the problem was resolved.

Always remember to access the file system on your Android device: -

DO NOT CONNECT LARGE STORAGE to your computer / PC.

+5
Oct 22 '14 at 10:17
source share

In my case, these were permission issues. The trick is that on a device with Android 4.0.4 I got access to the file without any error or exception. And on a device with Android 5.1, it crashed with the exception of ACCESS (open failed: EACCES (Permission denied)). Processed it by adding the following permissions for the manifest file:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

Therefore, I assume that this is the difference between managing permissions in OS versions, which leads to crashes.

+2
Jan 08 '17 at 14:29
source share

Give or check permissions first, e.g.

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 

If these two permissions are OK, then verify that the output streams are in the correct format.

Example:

 FileOutputStream fos=new FileOutputStream(Environment.getExternalStorageDirectory()+"/rahul1.jpg"); 
+2
Feb 21 '17 at 10:14
source share

I ran into the same problem and found that I should request permissions at runtime, even if I declared it in the manifest. As Justin Fiedler said.

The official documentation about this is here: https://developer.android.com/training/permissions/requesting.html

My implementation is slightly different from Justin Fiedler’s answer that he also implements a v4 fragment of the onRequestPermissionsResult method to process a request for permission request.

 public static final int REQUEST_EXTERNAL_PERMISSION_CODE = 666; @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) public static final String[] PERMISSIONS_EXTERNAL_STORAGE = { READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE }; public boolean checkExternalStoragePermission(Activity activity) { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) { return true; } int readStoragePermissionState = ContextCompat.checkSelfPermission(activity, READ_EXTERNAL_STORAGE); int writeStoragePermissionState = ContextCompat.checkSelfPermission(activity, WRITE_EXTERNAL_STORAGE); boolean externalStoragePermissionGranted = readStoragePermissionState == PackageManager.PERMISSION_GRANTED && writeStoragePermissionState == PackageManager.PERMISSION_GRANTED; if (!externalStoragePermissionGranted) { requestPermissions(PERMISSIONS_EXTERNAL_STORAGE, REQUEST_EXTERNAL_PERMISSION_CODE); } return externalStoragePermissionGranted; } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (requestCode == REQUEST_EXTERNAL_PERMISSION_CODE) { if (checkExternalStoragePermission(getActivity())) { // Continue with your action after permission request succeed } } } } 
+2
Apr 2 '17 at 1:04 on
source share

In my case, I used the android:isolatedProcess="true" option for the service in AndroidManifest.xml .

As soon as I deleted it, the error disappeared ...

+1
Mar 05 '15 at 19:40
source share

I also found a solution for my path.

Before launching the application, I granted root to the file explorer and did not disable write / read permission when exiting the application.

My application cannot use external memory while I recharged the device to reset all permissions.

+1
May 22 '15 at 7:26
source share

I have the same problem, but Sometimes the most difficult question gives a simple answer.

I recheck the manifest permissions and there WAS_NOT write permision shame me !!!

+1
Jun 19 '17 at 4:43 on
source share

If customers use Android 6.0, Android has added a new resolution model for (Marshmallow).

Trick: If you target version 22 or lower, your application will request all permissions during installation just like on any device running OS below Marshmallow

0
Apr 6 '16 at 22:51
source share

In my case, the problem was that the WIFI configuration, static, had a conflict with another device using the same IP address.

0
Feb 27 '17 at 7:34 on
source share

@ Uriel Frankel is right that access to Android 10 storage has changed. But the correct way is not to use the obsolete storage flag, but to request the storage of your application as follows:

 val screenShotDirPath = getApplication<Application>().getExternalFilesDir(Environment.DIRECTORY_PICTURES)?.path 

getExternalFilesDir is what you need.

0
01 Oct '19 at 15:02
source share

in my case, I forgot to add / in front of the file name after I added it, I got rid of it

 bitmap.compress(Bitmap.CompressFormat.PNG,100,new FileOutputStream(Environment.getExternalStorageDirectory()+"/arjunreddy.png")); 
-3
Aug 27 '17 at 7:39 on
source share



All Articles