In Lollipop, the download functionality works fine in my app, but when I upgraded to Marshmallow, my app crashes and gives this error when I try to download from the internet into the SD card:
Neither user nor current process has android.permission.WRITE_EXTERNAL_STORAGE
It complains about this line of code:
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
I have the permissions in the manifest outside application:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
I cleaned and rebuilt the project, but it still crashes.
Courtesy goes to Requesting Permissions at Run Time .
Example
Declare this as Global
Add this in your
onCreate()
sectionAfter setContentView(R.layout.your_xml);
Now adding checkPermission() and requestPermission()
FYI
onRequestPermissionsResult
You should be checking if the user has granted permission of external storage by using:
If not, you need to ask the user to grant your app a permission:
Of course these are for marshmallow devices only so you need to check if your app is running on Marshmallow:
Be also sure that your activity implements
OnRequestPermissionResult
The entire permission looks like this:
Permission result callback:
Check multiple Permission in API level 23 Step 1:
Step 2:
Step 3:
Step 4: in onCreate of Activity checkPermissions();
it's worked for me
Seems user has declined the permission and app tries to write to external disk, causing error.
Check https://developer.android.com/training/permissions/requesting.html
This video will give you a better idea about UX, handling Runtime permissions https://www.youtube.com/watch?v=iZqDdvhTZj0
Unless there is a definite requirement of writing on external storage, you can always choose to save files in app directory. In my case I had to save files and after wasting 2 to 3 days I found out if I change the storage path from
to
it works like charm on all the devices. This is because for writing on External storage you need extra permissions but writing in internal app directory is simple.