-1

I have an app that saves a file in pictures directory of the internal memory of the phone. I have this in my manifest:

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

In android documentation it says that you must request for permission in execution (not installation) in devices with android 6 or later. But I have tested it in three devices:

  • Moto E with android 7.0
  • Moto E with android 7.1
  • Huawei P8 Lite with android 6.0

And it saves the file just with the permission in the manifest, without requesting it. I would like to test the new code I have to add to handle this in a device that requires the permission in runtime, why these 3 devices don't require it?

takluiper
  • 679
  • 2
  • 8
  • 24
  • 1
    You likely have your `targetSdkVersion` set to <23. You have to target at least 23 for the runtime permissions. – Mike M. Jan 24 '18 at 10:56
  • just what Mike says your `targetSdkVersion` must be below 23 . add you build.gradle to confirm . – ADM Jan 24 '18 at 10:58
  • yes, it is below 23. Will it work on devices with android>23 without asking in runtime? – takluiper Jan 24 '18 at 11:01
  • Are you sure you are writing it to the external directory? Isn't it an internal directory that doesn't need that permission? – Eduardo Herzer Jan 24 '18 at 11:02
  • I get the directory with: Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) – takluiper Jan 24 '18 at 11:03
  • "Will it work on devices with android>23 without asking in runtime?" - Well, yeah, that's the situation you describe in your question. However, if you're publishing your apps on Google Play, be aware that they are eventually going to require that you target 26, and then you'll have to handle runtime permissions. – Mike M. Jan 24 '18 at 11:05
  • @takluiper It will not work without asking runtime permission for the device having android>23. – R G Jan 24 '18 at 11:07
  • According to the documentation, it shouldn't work. But it works in the 3 devices I tested – takluiper Jan 24 '18 at 11:10
  • What documentation are you referring to? "If the device is running Android 5.1 or lower, __or__ your app's target SDK is 22 or lower: If you list a dangerous permission in your manifest, the user has to grant the permission when they install the app..." - https://developer.android.com/training/permissions/requesting.html – Mike M. Jan 24 '18 at 11:13
  • This one's a little clearer: "If the device is running Android 6.0 (API level 23) or higher, _and_ the app's `targetSdkVersion` is 23 or higher, the app requests permissions from the user at run-time.", "If the device is running Android 5.1.1 (API level 22) or lower, _or_ the app's `targetSdkVersion` is 22 or lower, the system asks the user to grant the permissions when the user installs the app." https://developer.android.com/guide/topics/permissions/requesting.html#permissions – Mike M. Jan 24 '18 at 11:18
  • you are right. Then my question is solved, thank you. If you want to post it as a reply I'll check it as answered – takluiper Jan 24 '18 at 11:20
  • Actually, I'll just mark this as a duplicate, since it has been asked here many times before. Thanks, though. I appreciate the offer. Glad you got it worked out. Cheers! – Mike M. Jan 24 '18 at 11:33

1 Answers1

1

you will have to ask for the permission at runtime like this,

ActivityCompat.requestPermissions(EmployeeDetailsActivity.this,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA},
                REQUEST_CODE);
jitendra purohit
  • 652
  • 5
  • 18