Skip to content Skip to sidebar Skip to footer

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' ... minS

Solution 1:

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.

Solution 2:

When your app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps.

valpath= getExternalFilesDir(Environment.DIRECTORY_PICTURES.toString() + 
    File.separator + "Output Folder")
    valfile= File(getExternalFilesDir(null), "filename.jpg")

For more details visit Docs

Solution 3:

With Android version 10 (API level 29) the concept of scoped storage is introduced. According to Docs- "To give users more control over their files and to limit file clutter, apps that target Android 10 (API level 29) and higher are given scoped access into external storage, or scoped storage, by default. Such apps have access only to the app-specific directory on external storage, as well as specific types of media that the app has created."

for Android version 10, you have option to opt-out scoped storage by adding android:requestLegacyExternalStorage="true" in AndroidManifest.xml within the application tags

This is a temporary solution and will work only for android version 10 and not above it. Also make sure you ask run-time permission for read/write external storage.

Solution 4:

Android 11 will simply ignore android:requestLegacyExternalStorage="true", since it was an ad-hoc solution for Android < 11 to not break old apps.

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

and also in application tag of manifest file :

 android:requestLegacyExternalStorage="true"

also declare provider in application tag of manifest file like this :

<providerandroid:name="androidx.core.content.FileProvider"android:authorities="${applicationId}.provider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/provider_path" /></provider>

create a folder name xml in res folder. refer to this image

now create a xml file named provider_path.xml in xml folder you just created and paste below code in xml file :

<?xml version="1.0" encoding="utf-8"?><path><external-pathname="external_files"path="." />

now in your activity :

Filefile=newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+"sample.pdf");
                    if(file.exists()){
                        Uriuri= FileProvider.getUriForFile(context, "com.example.www"+".provider",file);
                        Intenti=newIntent(Intent.ACTION_VIEW);
                        i.setDataAndType(uri, "application/pdf");
                        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        context.startActivity(i);
                    }

replace com.example.www with your application package name.

Post a Comment for "Android Apiv29 Filenotfoundexception Eacces (permission Denied)"