Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need runtime permission check to write into getExternalFilesDir() path in marshmallow?

Tags:

android

In my android app, I save some files to Environment.getExternalStorageDirectory() + "\MyApp" directory. This worked fine until android 6 marshmallow update. After marshmallow update, I cannot write to this directory.

As described in this answer, in marshmallow, apps need to ask for the permission from user at runtime before writing to external storage.

But, when I use context.getExternalFilesDir(null) instead of Environment.getExternalStorageDirectory(), I don't need to ask for any permission at runtime and it just works (path returned from context.getExternalFilesDir(null) is also inside the external storage directory).

Is this some kind of a coincidence or can I continue to write to context.getExternalFilesDir(null) without asking permission at runtime?

like image 945
Lahiru Chandima Avatar asked Dec 26 '15 07:12

Lahiru Chandima


People also ask

What are runtime permissions in Marshmallow?

From MarshMallow android introduced runtime permissions, we can grant the permissions while using the app, instead of while installing the app. In general, If you want to use a camera or make a call from your app you will ask for the user’s permission.

How to make a txt file in external storage with runtime permission?

This example demonstrates How to make a txt file in external storage with runtime permission in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

How does the Xamarin Android app determine the path to files?

The Xamarin.Android app determines the path to the file that will be manipulated, then uses standard.NET idioms for file access. Because the actual paths to internal and external storage may vary from device to device or from Android version to Android version, it is not recommended to hard code the path to the files.

Will older Android apps run on Android Marshmallow?

With the introduction of Android 6.0 (SDK 23), users are prompted for some specific permissions at runtime when they become necessary to use. So the first question that comes to our mind is - Will the older apps run on Android Marshmallow? The answer is yes if the targetSdkVersion is 22 or less.


1 Answers

The documentation states:

Starting in KITKAT, no permissions are required to read or write to the returned path; it's always accessible to the calling app. This only applies to paths generated for package name of the calling application. To access paths belonging to other packages, WRITE_EXTERNAL_STORAGE and/or READ_EXTERNAL_STORAGE are required.

You will have read/write access to getExternalFilesDir() on Android 4.4+ without requiring any permissions.

I would recommend using a FileProvider if you need to support lower API levels.

like image 111
Jared Rummler Avatar answered Sep 22 '22 06:09

Jared Rummler