Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android APIv29 FileNotFoundException EACCES (Permission denied)

I'm not able to access storage when building for targetSdkVersion v29.

Here is my gradle configuration:

    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    ...
        minSdkVersion 15
        targetSdkVersion 29

NOTE that WRITE_EXTERNAL_STORAGE permission is granted and the same setup works fine when building for targetSdkVersion 28.

Here is my implementation:

        val outputFolder = File(baseFolder + File.separator + "Output Folder")
        if (!outputFolder.exists()) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Files.createDirectory(outputFolder.toPath()) //This allways returns false with targetSdkVersion 29
            } else {
                if (!outputFolder.mkdirs()) {
                    Log.e("SaveRaw", "Unable to create folder for audio recording")
                }
            }
        }

        outputFile = File("$baseFolder/Output Folder/$filename")
        try {
            fileOutputStream = FileOutputStream(outputFile)
        } catch (e: FileNotFoundException) {
            e.printStackTrace() // allways throwing exception here, even if Output Folder exists 
        }

and here is the exception:

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Chirp Auto Tester/2019_10_17 10:44:43.raw: open failed: EACCES (Permission denied)
W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:496)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:235)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:186)

Hope anyone has an answer, what am I missing here?

Update:

Here is where baseFolder comes from. Note that getExternalStorageDirectory is a deprecated method.

        val baseFolder: String = if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Environment.getExternalStorageDirectory().absolutePath
        } else {
            context.filesDir.absolutePath
        }

Thanks

like image 631
Dinu Avatar asked Oct 17 '19 10:10

Dinu


People also ask

How do I fix Open failed Eacces permission denied?

Just turn off USB storage, and with the correct permissions, you'll have the problem solved.

How do you fix exception Open failed Eacces permission denied on Android 11 12?

MANAGE_EXTERNAL_STORAGE"/> in the manifest file and try to get the file access permission in the android 11 phones. Then you will open and read the file from the storage. if you are working with android 11 and working on a file then you have to use MANAGE_EXTERNAL_STORAGE permission.

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.


1 Answers

Starting with Android 11 the storage permission is getting revoked and developers would need to consider alternative ways of accessing the storage they need either through SAF or Media Store. For the time being, you can carry on using what you’re using by adding the following in your manifest within the application tags:

android:requestLegacyExternalStorage="true"

You might want to consider changing your minSDK to 19 and use getExternalFilesDir() to get a path that doesn’t require any permissions.

like image 119
Nikos Hidalgo Avatar answered Nov 16 '22 00:11

Nikos Hidalgo