How to Add Read External Storage Permission in Android

Learn how the MANAGE_EXTERNAL_STORAGE permission works in android and how to implement it.

In the last days I've been working on the development of a Capacitor Plugin that allows to launch a native file picker and I faced a new problematic that a lof of android developers seems to exist having now. Since Android eleven (apps targeting SDK Version 30) which is past default the target API of a new Capacitor based applications, there's a new update on the Storage policies of apps. The Scoped Storage enforcement has been implemented to enhance the platform, giving better protection to your app and user's information on external storage. This will cause some modifications in which app works since this API equally:

  • On Android eleven, apps can no longer access files in any  other app'sdedicated, app-specific directory inside external storage.
  • To protect user privacy, on devices that run Android eleven or higher, the organisation further restricts your app'southward access to other apps' private directories.

The reason why I faced this trouble is because the plugin should allow to select any possible file in the device as it will be used inside a Code Editor application.

Accessing all files

There's a possibility to access all the files in the device using the new permission . All the same, this automatically will put a restriction in your application as Google Play should review if y'all actually need the mentioned permission when trying to publish the application to the Play Store.

The permission won't be applied automatically to your app without the user'south interaction as the user needs to obligatorily grant admission to the files. You lot can exercise this in Android following these steps:

1. Request MANAGE_EXTERNAL_STORAGE permission

Modify your AndroidManifest.xml file by requesting the following permissions:

          <manifest      xmlns:android="http://schemas.android.com/apk/res/android"      parcel="com.ourcodeworld.plugins.capacitornativefilepicker"     xmlns:tools="http://schemas.android.com/tools" >     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>     <uses-permission android:proper name="android.permission.READ_EXTERNAL_STORAGE"/>     <uses-permission android:proper noun="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage"/> </manifest>        

The MANAGE_EXTERNAL_STORAGE permission allows an application a wide access to external storage in scoped storage. Intended to be used by few apps that need to manage files on behalf of the users.

ii. Request External Storage Permissions

Later declaring the permissions, you need to asking the regular READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions. Note that we don't include the MANAGE_EXTERNAL_STORAGE, because if you request it, when verifying if you accept access or not, the mentioned permission will ever return denied, even though the user has already granted admission:

          ActivityCompat.requestPermissions(     this,     new String[]{         Manifest.permission.READ_EXTERNAL_STORAGE,         Manifest.permission.MANAGE_EXTERNAL_STORAGE     },     1 );        

The logic of this step is up to yous as you may know how to request permissions in your ain application.

iii. Checking permission of MANAGE_EXTERNAL_STORAGE

Now, instead of the regular permissions request to handle the mentioned permission, you need instead to verify whether the access to the external storage is immune. In example it isn't, you need to launch a new action showing the organisation'southward dialog where the user should manually allow the access to the external storage to your app like this:

          import android.os.Surround; import android.content.Intent; import android.provider.Settings; import android.net.Uri;  // If you accept admission to the external storage, do whatever you need if (Environment.isExternalStorageManager()){  // If y'all don't accept access, launch a new activity to bear witness the user the organisation's dialog // to permit admission to the external storage }else{     Intent intent = new Intent();     intent.setAction(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);     Uri uri = Uri.fromParts("package", this.getPackageName(), zippo);     intent.setData(uri);     startActivity(intent); }        

So when the user installs the app and try to access the file picker, if the admission to all files isn't granted withal, the following system intent will be launched:

Allow Access to all Files Android

The user will accept to explicitly permit admission to all files if y'all want to read files from whatever source in the device and that should be enough.

Happy coding ❤️!

moraleshoset1999.blogspot.com

Source: https://ourcodeworld.com/articles/read/1559/how-does-manage-external-storage-permission-work-in-android

0 Response to "How to Add Read External Storage Permission in Android"

ارسال یک نظر

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel