Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception 'open failed: EACCES (Permission denied)' on Android

I am getting

open failed: EACCES (Permission denied)

on the line OutputStream myOutput = new FileOutputStream(outFileName);

I checked the root, and I tried android.permission.WRITE_EXTERNAL_STORAGE.

How can I fix this problem?

try {     InputStream myInput;      myInput = getAssets().open("XXX.db");      // Path to the just created empty db     String outFileName = "/data/data/XX/databases/"             + "XXX.db";      // Open the empty db as the output stream     OutputStream myOutput = new FileOutputStream(outFileName);      // Transfer bytes from the inputfile to the outputfile     byte[] buffer = new byte[1024];     int length;     while ((length = myInput.read(buffer)) > 0) {         myOutput.write(buffer, 0, length);     }      // Close the streams     myOutput.flush();     myOutput.close();     myInput.close();     buffer = null;     outFileName = null; } catch (IOException e1) {     // TODO Auto-generated catch block     e1.printStackTrace(); } 
like image 311
Mert Avatar asked Jan 13 '12 17:01

Mert


People also ask

How to fix EACCES Permission denied Android?

What I found is that if you go to Settings -> Application Manager -> Your App -> Permissions -> Enable Storage, it solves the issue.

How do I fix Open failed Eacces permission denied?

FileNotFoundException open failed: EACCES (Permission denied) To fix this problem it is necessary to go to Settings – Apps – My App – Permission. Access to Storage is probably disabled.

What is Eacces permission denied?

It looks like you're running into permission issues. If you are installing npm-packages then it might possible that you are getting an EACCES error when trying to install a package globally. This means you do not have permission to write to the directories npm uses to store global packages and commands.

How do I give all access to a file in Android 11?

On the Settings > Privacy > Permission manager > Files and media page, each app that has the permission is listed under Allowed for all files. If your app targets Android 11, keep in mind that this access to "all files" is read-only.


2 Answers

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 Q. -->     <application android:requestLegacyExternalStorage="true" ... >      ...     </application> </manifest> 

You can read more about it here: https://developer.android.com/training/data-storage/use-cases

Edit: I am starting to get downvotes because this answer is out of date for Android 11. So whoever sees this answer please go to the link above and read the instructions.

like image 111
Uriel Frankel Avatar answered Sep 20 '22 15:09

Uriel Frankel


For API 23+ you need to request the read/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" /> 

For official documentation about requesting permissions for API 23+, check https://developer.android.com/training/permissions/requesting.html

like image 43
Justin Fiedler Avatar answered Sep 19 '22 15:09

Justin Fiedler