10

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?

Community
  • 1
  • 1
Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179

3 Answers3

12

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.

snkashis
  • 2,951
  • 1
  • 18
  • 35
Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
  • If I am targeting API 25, but supporting down to API 16, do I still need to declare the `WRITE_EXTERNAL_STORAGE` permission in the Manifest and then just not ask for the permission at runtime? – ankushg Dec 20 '16 at 00:30
1
<uses-permission
android:maxSdkVersion="18"
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

reference: uses-permission-element

Fan Wu
  • 11
  • 1
  • I have tested two device with Android 5.1. They are not working without permission, so change maxSdkVersion up to 22. – Style-7 Jun 27 '20 at 07:51
0

As i know PERMISSIONS are presented started since Android 6 and above So no need to check permissions for API 16

lelic.dev
  • 1
  • 2