READ_EXTERNAL_STORAGE permission for Android










68















I'm trying to access media files (music) on the users device to play them; an easy "hello world"-music player app.



I've followed some tutorials and they basically give the same code. But it won't work; it keeps crashing and telling me:



error.....
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=27696, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
....


Now, this is my manifest file:



<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="slimsimapps.troff" >

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


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>


This is my Java-method:



public void initialize() 
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null)
// query failed, handle error.
else if (!cursor.moveToFirst())
// no media on the device
else
do
addSongToXML(cursor);
while (cursor.moveToNext());




I have tried:



To put this at different places in the manifest file:



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


To add android:maxSdkVersion at Read external storage premission:



<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="21" />


To put this in the manifest / application / activity-tag:



android:exported=“true”


To put grantUriPremission between uri and cursro in the javamethod:



grantUriPermission(null, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);


To use this, it won't crash, but the cursor becomes null:



uri = MediaStore.Audio.Media.getContentUri("EXTERNAL_CONTENT_URI”);


To use INTERNAL content uri, this works as expected, but it only gives "OS-sounds" such as shutter-sound, low-battery-sound, button-click and such:



uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;


Pleas help, this should not be a hard problem i know, but i feel like such a beginner!



I have read and tried (or considered them to be not applicable for my problem):



  • Android READ_EXTERNAL_STORAGE permission not working

  • Require permission only for older android versions: maxSdkVersion does not work?

  • Get filename and path from URI from mediastore

  • Android KitKat securityException when trying to read from MediaStore

  • Android: java.lang.SecurityException: Permission Denial: start Intent

Stack trace:



09-08 06:59:36.619 2009-2009/slimsimapps.troff D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
09-08 06:59:36.619 2009-2009/slimsimapps.troff E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: slimsimapps.troff, PID: 2009
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
            at android.view.View.performClick(View.java:5198)
            at android.view.View$PerformClick.run(View.java:21147)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2009, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1599)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at android.content.ContentResolver.query(ContentResolver.java:491)
at android.content.ContentResolver.query(ContentResolver.java:434)
at slimsimapps.troff.MainActivity.initialize(MainActivity.java:106)
at slimsimapps.troff.MainActivity.InitializeExternal(MainActivity.java:80)
            at java.lang.reflect.Method.invoke(Native Method)
            at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
            at android.view.View.performClick(View.java:5198)
            at android.view.View$PerformClick.run(View.java:21147)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
--------- beginning of system









share|improve this question
























  • Strange that error is shown when you have not given this line in manifest <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> are you trying to read some files which is protected by some other apps?

    – sunil sunny
    Sep 7 '15 at 5:42












  • On which api level are you trying this ?

    – Sharp Edge
    Sep 7 '15 at 5:48











  • Did you tried cleaning the project. ?

    – sunil sunny
    Sep 7 '15 at 5:50











  • @sunilsunny I'm not trying to read som files that are protected, not that I know of anyway, just a simple media player. Yes, I have tried to clean it, i have tried to restart computer, I have tried to generate a signed APK and publish it to google Play and access it as a tester, with no luck....

    – Slim Sim
    Sep 7 '15 at 14:27











  • @SharpEdge ; My AVD is the standard Nexus 5, api 23. My module gradle has: compileSdkVersion 23 buildToolsVersion "23.0.0" minSdkVersion 14 targetSdkVersion 23 So i would say 23.

    – Slim Sim
    Sep 7 '15 at 14:29















68















I'm trying to access media files (music) on the users device to play them; an easy "hello world"-music player app.



I've followed some tutorials and they basically give the same code. But it won't work; it keeps crashing and telling me:



error.....
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=27696, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
....


Now, this is my manifest file:



<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="slimsimapps.troff" >

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


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>


This is my Java-method:



public void initialize() 
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null)
// query failed, handle error.
else if (!cursor.moveToFirst())
// no media on the device
else
do
addSongToXML(cursor);
while (cursor.moveToNext());




I have tried:



To put this at different places in the manifest file:



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


To add android:maxSdkVersion at Read external storage premission:



<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="21" />


To put this in the manifest / application / activity-tag:



android:exported=“true”


To put grantUriPremission between uri and cursro in the javamethod:



grantUriPermission(null, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);


To use this, it won't crash, but the cursor becomes null:



uri = MediaStore.Audio.Media.getContentUri("EXTERNAL_CONTENT_URI”);


To use INTERNAL content uri, this works as expected, but it only gives "OS-sounds" such as shutter-sound, low-battery-sound, button-click and such:



uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;


Pleas help, this should not be a hard problem i know, but i feel like such a beginner!



I have read and tried (or considered them to be not applicable for my problem):



  • Android READ_EXTERNAL_STORAGE permission not working

  • Require permission only for older android versions: maxSdkVersion does not work?

  • Get filename and path from URI from mediastore

  • Android KitKat securityException when trying to read from MediaStore

  • Android: java.lang.SecurityException: Permission Denial: start Intent

Stack trace:



09-08 06:59:36.619 2009-2009/slimsimapps.troff D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
09-08 06:59:36.619 2009-2009/slimsimapps.troff E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: slimsimapps.troff, PID: 2009
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
            at android.view.View.performClick(View.java:5198)
            at android.view.View$PerformClick.run(View.java:21147)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2009, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1599)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at android.content.ContentResolver.query(ContentResolver.java:491)
at android.content.ContentResolver.query(ContentResolver.java:434)
at slimsimapps.troff.MainActivity.initialize(MainActivity.java:106)
at slimsimapps.troff.MainActivity.InitializeExternal(MainActivity.java:80)
            at java.lang.reflect.Method.invoke(Native Method)
            at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
            at android.view.View.performClick(View.java:5198)
            at android.view.View$PerformClick.run(View.java:21147)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
--------- beginning of system









share|improve this question
























  • Strange that error is shown when you have not given this line in manifest <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> are you trying to read some files which is protected by some other apps?

    – sunil sunny
    Sep 7 '15 at 5:42












  • On which api level are you trying this ?

    – Sharp Edge
    Sep 7 '15 at 5:48











  • Did you tried cleaning the project. ?

    – sunil sunny
    Sep 7 '15 at 5:50











  • @sunilsunny I'm not trying to read som files that are protected, not that I know of anyway, just a simple media player. Yes, I have tried to clean it, i have tried to restart computer, I have tried to generate a signed APK and publish it to google Play and access it as a tester, with no luck....

    – Slim Sim
    Sep 7 '15 at 14:27











  • @SharpEdge ; My AVD is the standard Nexus 5, api 23. My module gradle has: compileSdkVersion 23 buildToolsVersion "23.0.0" minSdkVersion 14 targetSdkVersion 23 So i would say 23.

    – Slim Sim
    Sep 7 '15 at 14:29













68












68








68


20






I'm trying to access media files (music) on the users device to play them; an easy "hello world"-music player app.



I've followed some tutorials and they basically give the same code. But it won't work; it keeps crashing and telling me:



error.....
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=27696, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
....


Now, this is my manifest file:



<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="slimsimapps.troff" >

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


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>


This is my Java-method:



public void initialize() 
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null)
// query failed, handle error.
else if (!cursor.moveToFirst())
// no media on the device
else
do
addSongToXML(cursor);
while (cursor.moveToNext());




I have tried:



To put this at different places in the manifest file:



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


To add android:maxSdkVersion at Read external storage premission:



<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="21" />


To put this in the manifest / application / activity-tag:



android:exported=“true”


To put grantUriPremission between uri and cursro in the javamethod:



grantUriPermission(null, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);


To use this, it won't crash, but the cursor becomes null:



uri = MediaStore.Audio.Media.getContentUri("EXTERNAL_CONTENT_URI”);


To use INTERNAL content uri, this works as expected, but it only gives "OS-sounds" such as shutter-sound, low-battery-sound, button-click and such:



uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;


Pleas help, this should not be a hard problem i know, but i feel like such a beginner!



I have read and tried (or considered them to be not applicable for my problem):



  • Android READ_EXTERNAL_STORAGE permission not working

  • Require permission only for older android versions: maxSdkVersion does not work?

  • Get filename and path from URI from mediastore

  • Android KitKat securityException when trying to read from MediaStore

  • Android: java.lang.SecurityException: Permission Denial: start Intent

Stack trace:



09-08 06:59:36.619 2009-2009/slimsimapps.troff D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
09-08 06:59:36.619 2009-2009/slimsimapps.troff E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: slimsimapps.troff, PID: 2009
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
            at android.view.View.performClick(View.java:5198)
            at android.view.View$PerformClick.run(View.java:21147)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2009, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1599)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at android.content.ContentResolver.query(ContentResolver.java:491)
at android.content.ContentResolver.query(ContentResolver.java:434)
at slimsimapps.troff.MainActivity.initialize(MainActivity.java:106)
at slimsimapps.troff.MainActivity.InitializeExternal(MainActivity.java:80)
            at java.lang.reflect.Method.invoke(Native Method)
            at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
            at android.view.View.performClick(View.java:5198)
            at android.view.View$PerformClick.run(View.java:21147)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
--------- beginning of system









share|improve this question
















I'm trying to access media files (music) on the users device to play them; an easy "hello world"-music player app.



I've followed some tutorials and they basically give the same code. But it won't work; it keeps crashing and telling me:



error.....
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=27696, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
....


Now, this is my manifest file:



<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="slimsimapps.troff" >

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


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>


This is my Java-method:



public void initialize() 
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null)
// query failed, handle error.
else if (!cursor.moveToFirst())
// no media on the device
else
do
addSongToXML(cursor);
while (cursor.moveToNext());




I have tried:



To put this at different places in the manifest file:



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


To add android:maxSdkVersion at Read external storage premission:



<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="21" />


To put this in the manifest / application / activity-tag:



android:exported=“true”


To put grantUriPremission between uri and cursro in the javamethod:



grantUriPermission(null, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);


To use this, it won't crash, but the cursor becomes null:



uri = MediaStore.Audio.Media.getContentUri("EXTERNAL_CONTENT_URI”);


To use INTERNAL content uri, this works as expected, but it only gives "OS-sounds" such as shutter-sound, low-battery-sound, button-click and such:



uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;


Pleas help, this should not be a hard problem i know, but i feel like such a beginner!



I have read and tried (or considered them to be not applicable for my problem):



  • Android READ_EXTERNAL_STORAGE permission not working

  • Require permission only for older android versions: maxSdkVersion does not work?

  • Get filename and path from URI from mediastore

  • Android KitKat securityException when trying to read from MediaStore

  • Android: java.lang.SecurityException: Permission Denial: start Intent

Stack trace:



09-08 06:59:36.619 2009-2009/slimsimapps.troff D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
09-08 06:59:36.619 2009-2009/slimsimapps.troff E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: slimsimapps.troff, PID: 2009
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
            at android.view.View.performClick(View.java:5198)
            at android.view.View$PerformClick.run(View.java:21147)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2009, uid=10059 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1599)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at android.content.ContentResolver.query(ContentResolver.java:491)
at android.content.ContentResolver.query(ContentResolver.java:434)
at slimsimapps.troff.MainActivity.initialize(MainActivity.java:106)
at slimsimapps.troff.MainActivity.InitializeExternal(MainActivity.java:80)
            at java.lang.reflect.Method.invoke(Native Method)
            at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
            at android.view.View.performClick(View.java:5198)
            at android.view.View$PerformClick.run(View.java:21147)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
--------- beginning of system






java android music-player android-external-storage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 12:26









Community

11




11










asked Sep 7 '15 at 5:36









Slim SimSlim Sim

4831413




4831413












  • Strange that error is shown when you have not given this line in manifest <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> are you trying to read some files which is protected by some other apps?

    – sunil sunny
    Sep 7 '15 at 5:42












  • On which api level are you trying this ?

    – Sharp Edge
    Sep 7 '15 at 5:48











  • Did you tried cleaning the project. ?

    – sunil sunny
    Sep 7 '15 at 5:50











  • @sunilsunny I'm not trying to read som files that are protected, not that I know of anyway, just a simple media player. Yes, I have tried to clean it, i have tried to restart computer, I have tried to generate a signed APK and publish it to google Play and access it as a tester, with no luck....

    – Slim Sim
    Sep 7 '15 at 14:27











  • @SharpEdge ; My AVD is the standard Nexus 5, api 23. My module gradle has: compileSdkVersion 23 buildToolsVersion "23.0.0" minSdkVersion 14 targetSdkVersion 23 So i would say 23.

    – Slim Sim
    Sep 7 '15 at 14:29

















  • Strange that error is shown when you have not given this line in manifest <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> are you trying to read some files which is protected by some other apps?

    – sunil sunny
    Sep 7 '15 at 5:42












  • On which api level are you trying this ?

    – Sharp Edge
    Sep 7 '15 at 5:48











  • Did you tried cleaning the project. ?

    – sunil sunny
    Sep 7 '15 at 5:50











  • @sunilsunny I'm not trying to read som files that are protected, not that I know of anyway, just a simple media player. Yes, I have tried to clean it, i have tried to restart computer, I have tried to generate a signed APK and publish it to google Play and access it as a tester, with no luck....

    – Slim Sim
    Sep 7 '15 at 14:27











  • @SharpEdge ; My AVD is the standard Nexus 5, api 23. My module gradle has: compileSdkVersion 23 buildToolsVersion "23.0.0" minSdkVersion 14 targetSdkVersion 23 So i would say 23.

    – Slim Sim
    Sep 7 '15 at 14:29
















Strange that error is shown when you have not given this line in manifest <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> are you trying to read some files which is protected by some other apps?

– sunil sunny
Sep 7 '15 at 5:42






Strange that error is shown when you have not given this line in manifest <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> are you trying to read some files which is protected by some other apps?

– sunil sunny
Sep 7 '15 at 5:42














On which api level are you trying this ?

– Sharp Edge
Sep 7 '15 at 5:48





On which api level are you trying this ?

– Sharp Edge
Sep 7 '15 at 5:48













Did you tried cleaning the project. ?

– sunil sunny
Sep 7 '15 at 5:50





Did you tried cleaning the project. ?

– sunil sunny
Sep 7 '15 at 5:50













@sunilsunny I'm not trying to read som files that are protected, not that I know of anyway, just a simple media player. Yes, I have tried to clean it, i have tried to restart computer, I have tried to generate a signed APK and publish it to google Play and access it as a tester, with no luck....

– Slim Sim
Sep 7 '15 at 14:27





@sunilsunny I'm not trying to read som files that are protected, not that I know of anyway, just a simple media player. Yes, I have tried to clean it, i have tried to restart computer, I have tried to generate a signed APK and publish it to google Play and access it as a tester, with no luck....

– Slim Sim
Sep 7 '15 at 14:27













@SharpEdge ; My AVD is the standard Nexus 5, api 23. My module gradle has: compileSdkVersion 23 buildToolsVersion "23.0.0" minSdkVersion 14 targetSdkVersion 23 So i would say 23.

– Slim Sim
Sep 7 '15 at 14:29





@SharpEdge ; My AVD is the standard Nexus 5, api 23. My module gradle has: compileSdkVersion 23 buildToolsVersion "23.0.0" minSdkVersion 14 targetSdkVersion 23 So i would say 23.

– Slim Sim
Sep 7 '15 at 14:29












8 Answers
8






active

oldest

votes


















65














You have two solutions for your problem. The quick one is to lower targetApi to 22 (build.gradle file).
Second is to use new and wonderful ask-for-permission model:



if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)

// Should we show an explanation?
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_EXTERNAL_STORAGE))
// Explain to the user why we need to read the contacts


requestPermissions(new StringManifest.permission.READ_EXTERNAL_STORAGE,
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

// MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
// app-defined int constant that should be quite unique

return;



Sniplet found here: https://developer.android.com/training/permissions/requesting.html






share|improve this answer




















  • 1





    Yes, of course! The new permission model Google introduced with the latest Android! Thank you, I will test this as soon as possible and mark it correct if it works! (and i was beginning to think this was forgotten!)

    – Slim Sim
    Sep 17 '15 at 15:47











  • The permission that is being requested should be READ_EXTERNAL_STORAGE. Like in this answer.

    – maclir
    Mar 1 '16 at 10:12











  • swap != PackageManager.READ_EXTERNAL_STORAGE for != PackageManager.PERMISSION_GRANTED

    – Renascienza
    Nov 10 '16 at 17:51







  • 1





    @Renascienza, yes you're right. Someone edited this answer in wrong way.

    – piotrpo
    Nov 14 '16 at 18:31






  • 1





    What is MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE ?

    – Solo
    Aug 5 '17 at 10:32


















13














You have ask for the permission at run time:



if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new StringManifest.permission.READ_EXTERNAL_STORAGE,
REQUEST_PERMISSION);
dialog.dismiss();
return;



And in the callback below you can access the storage without a problem.



@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull final String permissions, @NonNull final int grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION)
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
// Permission granted.
else
// User refused to grant permission.








share|improve this answer






























    5














    Step1: add permission on android manifest.xml



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


    Step2: onCreate() method



    int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permissionCheck != PackageManager.PERMISSION_GRANTED)
    ActivityCompat.requestPermissions(this, new StringManifest.permission.WRITE_EXTERNAL_STORAGE, MY_PERMISSIONS_REQUEST_READ_MEDIA);
    else
    readDataExternal();



    Step3: override onRequestPermissionsResult method to get callback



     @Override
    public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
    switch (requestCode)
    case MY_PERMISSIONS_REQUEST_READ_MEDIA:
    if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED))
    readDataExternal();

    break;

    default:
    break;




    Note: readDataExternal() is method to get data from external storage.



    Thanks.






    share|improve this answer


















    • 1





      I think you misplaced the needed permission here: is READ_EXTERNAL_STORAGE.

      – Renascienza
      Nov 10 '16 at 17:48











    • yes, thank you so much.

      – sonnv1368
      Nov 11 '16 at 6:49


















    4














    I also had a similar error log and here's what I did-




    1. In onCreate method we request a Dialog Box for checking permissions



      ActivityCompat.requestPermissions(MainActivity.this,
      new StringManifest.permission.READ_EXTERNAL_STORAGE,1);



    2. Method to check for the result



      @Override
      public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
      switch (requestCode)
      case 1:
      // If request is cancelled, the result arrays are empty.
      if (grantResults.length > 0
      && grantResults[0] == PackageManager.PERMISSION_GRANTED)
      // permission granted and now can proceed
      mymethod(); //a sample method called

      else

      // permission denied, boo! Disable the
      // functionality that depends on this permission.
      Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();

      return;

      // add other cases for more permissions




    The official documentation to Requesting Runtime Permissions






    share|improve this answer


















    • 1





      i used it and it worked perfectly does it work on all android devices ?

      – Mahdi Hraybi
      Aug 12 '17 at 22:19











    • yes it does work on all devices

      – user7020107
      Aug 29 '17 at 14:43


















    1














    Has your problem been resolved? What is your target SDK? Try adding android;maxSDKVersion="21" to <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />






    share|improve this answer























    • Worked for me using Cordova, by setting only the max SDK version in AndroidManifest.xml to 22 (instead of 23)

      – George Kagan
      Jun 6 '16 at 16:46











    • Does your app still work on Marshmallow and above?

      – intrepidis
      Jul 8 '17 at 11:52


















    0














    http://developer.android.com/reference/android/provider/MediaStore.Audio.AudioColumns.html



    Try changing the line contentResolver.query



    //change it to the columns you need
    String columns = new StringMediaStore.Audio.AudioColumns.DATA;
    Cursor cursor = contentResolver.query(uri, columns, null, null, null);



    public static final String MEDIA_CONTENT_CONTROL



    Not for use by third-party applications due to privacy of media consumption




    http://developer.android.com/reference/android/Manifest.permission.html#MEDIA_CONTENT_CONTROL



    try to remove the <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/> first and try again?






    share|improve this answer

























    • Thanks, I tried your suggestion but it crashed on the same spot... I'm using a Mac, can that be a problem?

      – Slim Sim
      Sep 7 '15 at 14:39











    • updated, try to remove <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/> and try? It may create strange behaviour because your app request for an permission not allowed

      – Derek Fung
      Sep 7 '15 at 15:25











    • thanks, i tried to remove that permission, but it crashes at the same spot with the same error...

      – Slim Sim
      Sep 8 '15 at 5:11



















    0














    Please Check below code that using that You can find all Music Files from sdcard :



    public class MainActivity extends Activity

    @Override
    protected void onCreate(Bundle savedInstanceState)
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_animations);
    getAllSongsFromSDCARD();



    public void getAllSongsFromSDCARD()
    String STAR = "*" ;
    Cursor cursor;
    Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

    cursor = managedQuery(allsongsuri, STAR, selection, null, null);

    if (cursor != null)
    if (cursor.moveToFirst())
    do
    String song_name = cursor
    .getString(cursor
    .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
    int song_id = cursor.getInt(cursor
    .getColumnIndex(MediaStore.Audio.Media._ID));

    String fullpath = cursor.getString(cursor
    .getColumnIndex(MediaStore.Audio.Media.DATA));

    String album_name = cursor.getString(cursor
    .getColumnIndex(MediaStore.Audio.Media.ALBUM));
    int album_id = cursor.getInt(cursor
    .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

    String artist_name = cursor.getString(cursor
    .getColumnIndex(MediaStore.Audio.Media.ARTIST));
    int artist_id = cursor.getInt(cursor
    .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
    System.out.println("sonng name"+fullpath);
    while (cursor.moveToNext());


    cursor.close();







    I have also added following line in the AndroidManifest.xml file as below:



    <uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="17" />

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





    share|improve this answer

























    • Thanks, I have tried something similar before, but I tried your code and it crashes on "cursor = managedQuery(allsongsuri, STAR, selection, null, null);", same error as before. By the way; managedQuery is deprecated, but it still crashes if i change it to "contentResolver.query", which have the same input parameters...

      – Slim Sim
      Sep 7 '15 at 14:32











    • Hi I am able to get All sdcadr Songs using above code. with Android Version 4.2.2 Device and also able to with Version 5.0.2. In which You are trying. What Error you got please describe with Android Version

      – Rajan Bhavsar
      Sep 8 '15 at 4:19











    • I have updated the question with the stack-trace, hope that helps. What do you mean with Android Version? (I'm using apk 23)

      – Slim Sim
      Sep 8 '15 at 5:18











    • Ok Let me Give Full Source code and Check Out that again.

      – Rajan Bhavsar
      Sep 8 '15 at 5:56











    • if you want to test for yourself, download the zip here: drive.google.com/open?id=0B2AV8VRk9HUeVzFXSWVRZU9TRUk when you run it, press the left initialize-button, to generate the crach.

      – Slim Sim
      Sep 8 '15 at 15:42



















    0














    In addition of all answers.
    You can also specify the minsdk to apply with this annotation



    @TargetApi(_apiLevel_)


    I used this in order to accept this request even my minsdk is 18. What it does is that the method only runs when device targets "_apilevel_" and upper.
    Here's my method:



     @TargetApi(23)
    void solicitarPermisos()

    if (ContextCompat.checkSelfPermission(this,permiso)
    != PackageManager.PERMISSION_GRANTED)

    // Should we show an explanation?
    if (shouldShowRequestPermissionRationale(
    Manifest.permission.READ_EXTERNAL_STORAGE))
    // Explain to the user why we need to read the contacts


    requestPermissions(new StringManifest.permission.READ_EXTERNAL_STORAGE,
    1);

    // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
    // app-defined int constant that should be quite unique

    return;








    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32431723%2fread-external-storage-permission-for-android%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      8 Answers
      8






      active

      oldest

      votes








      8 Answers
      8






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      65














      You have two solutions for your problem. The quick one is to lower targetApi to 22 (build.gradle file).
      Second is to use new and wonderful ask-for-permission model:



      if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
      != PackageManager.PERMISSION_GRANTED)

      // Should we show an explanation?
      if (shouldShowRequestPermissionRationale(
      Manifest.permission.READ_EXTERNAL_STORAGE))
      // Explain to the user why we need to read the contacts


      requestPermissions(new StringManifest.permission.READ_EXTERNAL_STORAGE,
      MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

      // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
      // app-defined int constant that should be quite unique

      return;



      Sniplet found here: https://developer.android.com/training/permissions/requesting.html






      share|improve this answer




















      • 1





        Yes, of course! The new permission model Google introduced with the latest Android! Thank you, I will test this as soon as possible and mark it correct if it works! (and i was beginning to think this was forgotten!)

        – Slim Sim
        Sep 17 '15 at 15:47











      • The permission that is being requested should be READ_EXTERNAL_STORAGE. Like in this answer.

        – maclir
        Mar 1 '16 at 10:12











      • swap != PackageManager.READ_EXTERNAL_STORAGE for != PackageManager.PERMISSION_GRANTED

        – Renascienza
        Nov 10 '16 at 17:51







      • 1





        @Renascienza, yes you're right. Someone edited this answer in wrong way.

        – piotrpo
        Nov 14 '16 at 18:31






      • 1





        What is MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE ?

        – Solo
        Aug 5 '17 at 10:32















      65














      You have two solutions for your problem. The quick one is to lower targetApi to 22 (build.gradle file).
      Second is to use new and wonderful ask-for-permission model:



      if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
      != PackageManager.PERMISSION_GRANTED)

      // Should we show an explanation?
      if (shouldShowRequestPermissionRationale(
      Manifest.permission.READ_EXTERNAL_STORAGE))
      // Explain to the user why we need to read the contacts


      requestPermissions(new StringManifest.permission.READ_EXTERNAL_STORAGE,
      MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

      // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
      // app-defined int constant that should be quite unique

      return;



      Sniplet found here: https://developer.android.com/training/permissions/requesting.html






      share|improve this answer




















      • 1





        Yes, of course! The new permission model Google introduced with the latest Android! Thank you, I will test this as soon as possible and mark it correct if it works! (and i was beginning to think this was forgotten!)

        – Slim Sim
        Sep 17 '15 at 15:47











      • The permission that is being requested should be READ_EXTERNAL_STORAGE. Like in this answer.

        – maclir
        Mar 1 '16 at 10:12











      • swap != PackageManager.READ_EXTERNAL_STORAGE for != PackageManager.PERMISSION_GRANTED

        – Renascienza
        Nov 10 '16 at 17:51







      • 1





        @Renascienza, yes you're right. Someone edited this answer in wrong way.

        – piotrpo
        Nov 14 '16 at 18:31






      • 1





        What is MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE ?

        – Solo
        Aug 5 '17 at 10:32













      65












      65








      65







      You have two solutions for your problem. The quick one is to lower targetApi to 22 (build.gradle file).
      Second is to use new and wonderful ask-for-permission model:



      if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
      != PackageManager.PERMISSION_GRANTED)

      // Should we show an explanation?
      if (shouldShowRequestPermissionRationale(
      Manifest.permission.READ_EXTERNAL_STORAGE))
      // Explain to the user why we need to read the contacts


      requestPermissions(new StringManifest.permission.READ_EXTERNAL_STORAGE,
      MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

      // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
      // app-defined int constant that should be quite unique

      return;



      Sniplet found here: https://developer.android.com/training/permissions/requesting.html






      share|improve this answer















      You have two solutions for your problem. The quick one is to lower targetApi to 22 (build.gradle file).
      Second is to use new and wonderful ask-for-permission model:



      if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
      != PackageManager.PERMISSION_GRANTED)

      // Should we show an explanation?
      if (shouldShowRequestPermissionRationale(
      Manifest.permission.READ_EXTERNAL_STORAGE))
      // Explain to the user why we need to read the contacts


      requestPermissions(new StringManifest.permission.READ_EXTERNAL_STORAGE,
      MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);

      // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
      // app-defined int constant that should be quite unique

      return;



      Sniplet found here: https://developer.android.com/training/permissions/requesting.html







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 2 '17 at 8:04

























      answered Sep 16 '15 at 19:57









      piotrpopiotrpo

      9,23873151




      9,23873151







      • 1





        Yes, of course! The new permission model Google introduced with the latest Android! Thank you, I will test this as soon as possible and mark it correct if it works! (and i was beginning to think this was forgotten!)

        – Slim Sim
        Sep 17 '15 at 15:47











      • The permission that is being requested should be READ_EXTERNAL_STORAGE. Like in this answer.

        – maclir
        Mar 1 '16 at 10:12











      • swap != PackageManager.READ_EXTERNAL_STORAGE for != PackageManager.PERMISSION_GRANTED

        – Renascienza
        Nov 10 '16 at 17:51







      • 1





        @Renascienza, yes you're right. Someone edited this answer in wrong way.

        – piotrpo
        Nov 14 '16 at 18:31






      • 1





        What is MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE ?

        – Solo
        Aug 5 '17 at 10:32












      • 1





        Yes, of course! The new permission model Google introduced with the latest Android! Thank you, I will test this as soon as possible and mark it correct if it works! (and i was beginning to think this was forgotten!)

        – Slim Sim
        Sep 17 '15 at 15:47











      • The permission that is being requested should be READ_EXTERNAL_STORAGE. Like in this answer.

        – maclir
        Mar 1 '16 at 10:12











      • swap != PackageManager.READ_EXTERNAL_STORAGE for != PackageManager.PERMISSION_GRANTED

        – Renascienza
        Nov 10 '16 at 17:51







      • 1





        @Renascienza, yes you're right. Someone edited this answer in wrong way.

        – piotrpo
        Nov 14 '16 at 18:31






      • 1





        What is MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE ?

        – Solo
        Aug 5 '17 at 10:32







      1




      1





      Yes, of course! The new permission model Google introduced with the latest Android! Thank you, I will test this as soon as possible and mark it correct if it works! (and i was beginning to think this was forgotten!)

      – Slim Sim
      Sep 17 '15 at 15:47





      Yes, of course! The new permission model Google introduced with the latest Android! Thank you, I will test this as soon as possible and mark it correct if it works! (and i was beginning to think this was forgotten!)

      – Slim Sim
      Sep 17 '15 at 15:47













      The permission that is being requested should be READ_EXTERNAL_STORAGE. Like in this answer.

      – maclir
      Mar 1 '16 at 10:12





      The permission that is being requested should be READ_EXTERNAL_STORAGE. Like in this answer.

      – maclir
      Mar 1 '16 at 10:12













      swap != PackageManager.READ_EXTERNAL_STORAGE for != PackageManager.PERMISSION_GRANTED

      – Renascienza
      Nov 10 '16 at 17:51






      swap != PackageManager.READ_EXTERNAL_STORAGE for != PackageManager.PERMISSION_GRANTED

      – Renascienza
      Nov 10 '16 at 17:51





      1




      1





      @Renascienza, yes you're right. Someone edited this answer in wrong way.

      – piotrpo
      Nov 14 '16 at 18:31





      @Renascienza, yes you're right. Someone edited this answer in wrong way.

      – piotrpo
      Nov 14 '16 at 18:31




      1




      1





      What is MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE ?

      – Solo
      Aug 5 '17 at 10:32





      What is MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE ?

      – Solo
      Aug 5 '17 at 10:32













      13














      You have ask for the permission at run time:



      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
      && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
      ActivityCompat.requestPermissions(this, new StringManifest.permission.READ_EXTERNAL_STORAGE,
      REQUEST_PERMISSION);
      dialog.dismiss();
      return;



      And in the callback below you can access the storage without a problem.



      @Override
      public void onRequestPermissionsResult(final int requestCode, @NonNull final String permissions, @NonNull final int grantResults)
      super.onRequestPermissionsResult(requestCode, permissions, grantResults);
      if (requestCode == REQUEST_PERMISSION)
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
      // Permission granted.
      else
      // User refused to grant permission.








      share|improve this answer



























        13














        You have ask for the permission at run time:



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
        && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(this, new StringManifest.permission.READ_EXTERNAL_STORAGE,
        REQUEST_PERMISSION);
        dialog.dismiss();
        return;



        And in the callback below you can access the storage without a problem.



        @Override
        public void onRequestPermissionsResult(final int requestCode, @NonNull final String permissions, @NonNull final int grantResults)
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == REQUEST_PERMISSION)
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
        // Permission granted.
        else
        // User refused to grant permission.








        share|improve this answer

























          13












          13








          13







          You have ask for the permission at run time:



          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
          && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
          ActivityCompat.requestPermissions(this, new StringManifest.permission.READ_EXTERNAL_STORAGE,
          REQUEST_PERMISSION);
          dialog.dismiss();
          return;



          And in the callback below you can access the storage without a problem.



          @Override
          public void onRequestPermissionsResult(final int requestCode, @NonNull final String permissions, @NonNull final int grantResults)
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          if (requestCode == REQUEST_PERMISSION)
          if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
          // Permission granted.
          else
          // User refused to grant permission.








          share|improve this answer













          You have ask for the permission at run time:



          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
          && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
          ActivityCompat.requestPermissions(this, new StringManifest.permission.READ_EXTERNAL_STORAGE,
          REQUEST_PERMISSION);
          dialog.dismiss();
          return;



          And in the callback below you can access the storage without a problem.



          @Override
          public void onRequestPermissionsResult(final int requestCode, @NonNull final String permissions, @NonNull final int grantResults)
          super.onRequestPermissionsResult(requestCode, permissions, grantResults);
          if (requestCode == REQUEST_PERMISSION)
          if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
          // Permission granted.
          else
          // User refused to grant permission.









          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 1 '16 at 10:11









          maclirmaclir

          2,3291831




          2,3291831





















              5














              Step1: add permission on android manifest.xml



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


              Step2: onCreate() method



              int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

              if (permissionCheck != PackageManager.PERMISSION_GRANTED)
              ActivityCompat.requestPermissions(this, new StringManifest.permission.WRITE_EXTERNAL_STORAGE, MY_PERMISSIONS_REQUEST_READ_MEDIA);
              else
              readDataExternal();



              Step3: override onRequestPermissionsResult method to get callback



               @Override
              public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
              switch (requestCode)
              case MY_PERMISSIONS_REQUEST_READ_MEDIA:
              if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED))
              readDataExternal();

              break;

              default:
              break;




              Note: readDataExternal() is method to get data from external storage.



              Thanks.






              share|improve this answer


















              • 1





                I think you misplaced the needed permission here: is READ_EXTERNAL_STORAGE.

                – Renascienza
                Nov 10 '16 at 17:48











              • yes, thank you so much.

                – sonnv1368
                Nov 11 '16 at 6:49















              5














              Step1: add permission on android manifest.xml



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


              Step2: onCreate() method



              int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

              if (permissionCheck != PackageManager.PERMISSION_GRANTED)
              ActivityCompat.requestPermissions(this, new StringManifest.permission.WRITE_EXTERNAL_STORAGE, MY_PERMISSIONS_REQUEST_READ_MEDIA);
              else
              readDataExternal();



              Step3: override onRequestPermissionsResult method to get callback



               @Override
              public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
              switch (requestCode)
              case MY_PERMISSIONS_REQUEST_READ_MEDIA:
              if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED))
              readDataExternal();

              break;

              default:
              break;




              Note: readDataExternal() is method to get data from external storage.



              Thanks.






              share|improve this answer


















              • 1





                I think you misplaced the needed permission here: is READ_EXTERNAL_STORAGE.

                – Renascienza
                Nov 10 '16 at 17:48











              • yes, thank you so much.

                – sonnv1368
                Nov 11 '16 at 6:49













              5












              5








              5







              Step1: add permission on android manifest.xml



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


              Step2: onCreate() method



              int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

              if (permissionCheck != PackageManager.PERMISSION_GRANTED)
              ActivityCompat.requestPermissions(this, new StringManifest.permission.WRITE_EXTERNAL_STORAGE, MY_PERMISSIONS_REQUEST_READ_MEDIA);
              else
              readDataExternal();



              Step3: override onRequestPermissionsResult method to get callback



               @Override
              public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
              switch (requestCode)
              case MY_PERMISSIONS_REQUEST_READ_MEDIA:
              if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED))
              readDataExternal();

              break;

              default:
              break;




              Note: readDataExternal() is method to get data from external storage.



              Thanks.






              share|improve this answer













              Step1: add permission on android manifest.xml



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


              Step2: onCreate() method



              int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

              if (permissionCheck != PackageManager.PERMISSION_GRANTED)
              ActivityCompat.requestPermissions(this, new StringManifest.permission.WRITE_EXTERNAL_STORAGE, MY_PERMISSIONS_REQUEST_READ_MEDIA);
              else
              readDataExternal();



              Step3: override onRequestPermissionsResult method to get callback



               @Override
              public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
              switch (requestCode)
              case MY_PERMISSIONS_REQUEST_READ_MEDIA:
              if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED))
              readDataExternal();

              break;

              default:
              break;




              Note: readDataExternal() is method to get data from external storage.



              Thanks.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jul 28 '16 at 11:30









              sonnv1368sonnv1368

              1,209817




              1,209817







              • 1





                I think you misplaced the needed permission here: is READ_EXTERNAL_STORAGE.

                – Renascienza
                Nov 10 '16 at 17:48











              • yes, thank you so much.

                – sonnv1368
                Nov 11 '16 at 6:49












              • 1





                I think you misplaced the needed permission here: is READ_EXTERNAL_STORAGE.

                – Renascienza
                Nov 10 '16 at 17:48











              • yes, thank you so much.

                – sonnv1368
                Nov 11 '16 at 6:49







              1




              1





              I think you misplaced the needed permission here: is READ_EXTERNAL_STORAGE.

              – Renascienza
              Nov 10 '16 at 17:48





              I think you misplaced the needed permission here: is READ_EXTERNAL_STORAGE.

              – Renascienza
              Nov 10 '16 at 17:48













              yes, thank you so much.

              – sonnv1368
              Nov 11 '16 at 6:49





              yes, thank you so much.

              – sonnv1368
              Nov 11 '16 at 6:49











              4














              I also had a similar error log and here's what I did-




              1. In onCreate method we request a Dialog Box for checking permissions



                ActivityCompat.requestPermissions(MainActivity.this,
                new StringManifest.permission.READ_EXTERNAL_STORAGE,1);



              2. Method to check for the result



                @Override
                public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
                switch (requestCode)
                case 1:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                // permission granted and now can proceed
                mymethod(); //a sample method called

                else

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();

                return;

                // add other cases for more permissions




              The official documentation to Requesting Runtime Permissions






              share|improve this answer


















              • 1





                i used it and it worked perfectly does it work on all android devices ?

                – Mahdi Hraybi
                Aug 12 '17 at 22:19











              • yes it does work on all devices

                – user7020107
                Aug 29 '17 at 14:43















              4














              I also had a similar error log and here's what I did-




              1. In onCreate method we request a Dialog Box for checking permissions



                ActivityCompat.requestPermissions(MainActivity.this,
                new StringManifest.permission.READ_EXTERNAL_STORAGE,1);



              2. Method to check for the result



                @Override
                public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
                switch (requestCode)
                case 1:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                // permission granted and now can proceed
                mymethod(); //a sample method called

                else

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();

                return;

                // add other cases for more permissions




              The official documentation to Requesting Runtime Permissions






              share|improve this answer


















              • 1





                i used it and it worked perfectly does it work on all android devices ?

                – Mahdi Hraybi
                Aug 12 '17 at 22:19











              • yes it does work on all devices

                – user7020107
                Aug 29 '17 at 14:43













              4












              4








              4







              I also had a similar error log and here's what I did-




              1. In onCreate method we request a Dialog Box for checking permissions



                ActivityCompat.requestPermissions(MainActivity.this,
                new StringManifest.permission.READ_EXTERNAL_STORAGE,1);



              2. Method to check for the result



                @Override
                public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
                switch (requestCode)
                case 1:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                // permission granted and now can proceed
                mymethod(); //a sample method called

                else

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();

                return;

                // add other cases for more permissions




              The official documentation to Requesting Runtime Permissions






              share|improve this answer













              I also had a similar error log and here's what I did-




              1. In onCreate method we request a Dialog Box for checking permissions



                ActivityCompat.requestPermissions(MainActivity.this,
                new StringManifest.permission.READ_EXTERNAL_STORAGE,1);



              2. Method to check for the result



                @Override
                public void onRequestPermissionsResult(int requestCode, String permissions, int grantResults)
                switch (requestCode)
                case 1:
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                // permission granted and now can proceed
                mymethod(); //a sample method called

                else

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Toast.makeText(MainActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show();

                return;

                // add other cases for more permissions




              The official documentation to Requesting Runtime Permissions







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Dec 4 '16 at 14:46







              user7020107














              • 1





                i used it and it worked perfectly does it work on all android devices ?

                – Mahdi Hraybi
                Aug 12 '17 at 22:19











              • yes it does work on all devices

                – user7020107
                Aug 29 '17 at 14:43












              • 1





                i used it and it worked perfectly does it work on all android devices ?

                – Mahdi Hraybi
                Aug 12 '17 at 22:19











              • yes it does work on all devices

                – user7020107
                Aug 29 '17 at 14:43







              1




              1





              i used it and it worked perfectly does it work on all android devices ?

              – Mahdi Hraybi
              Aug 12 '17 at 22:19





              i used it and it worked perfectly does it work on all android devices ?

              – Mahdi Hraybi
              Aug 12 '17 at 22:19













              yes it does work on all devices

              – user7020107
              Aug 29 '17 at 14:43





              yes it does work on all devices

              – user7020107
              Aug 29 '17 at 14:43











              1














              Has your problem been resolved? What is your target SDK? Try adding android;maxSDKVersion="21" to <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />






              share|improve this answer























              • Worked for me using Cordova, by setting only the max SDK version in AndroidManifest.xml to 22 (instead of 23)

                – George Kagan
                Jun 6 '16 at 16:46











              • Does your app still work on Marshmallow and above?

                – intrepidis
                Jul 8 '17 at 11:52















              1














              Has your problem been resolved? What is your target SDK? Try adding android;maxSDKVersion="21" to <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />






              share|improve this answer























              • Worked for me using Cordova, by setting only the max SDK version in AndroidManifest.xml to 22 (instead of 23)

                – George Kagan
                Jun 6 '16 at 16:46











              • Does your app still work on Marshmallow and above?

                – intrepidis
                Jul 8 '17 at 11:52













              1












              1








              1







              Has your problem been resolved? What is your target SDK? Try adding android;maxSDKVersion="21" to <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />






              share|improve this answer













              Has your problem been resolved? What is your target SDK? Try adding android;maxSDKVersion="21" to <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 27 '16 at 3:16









              Jerry EjonaviJerry Ejonavi

              6117




              6117












              • Worked for me using Cordova, by setting only the max SDK version in AndroidManifest.xml to 22 (instead of 23)

                – George Kagan
                Jun 6 '16 at 16:46











              • Does your app still work on Marshmallow and above?

                – intrepidis
                Jul 8 '17 at 11:52

















              • Worked for me using Cordova, by setting only the max SDK version in AndroidManifest.xml to 22 (instead of 23)

                – George Kagan
                Jun 6 '16 at 16:46











              • Does your app still work on Marshmallow and above?

                – intrepidis
                Jul 8 '17 at 11:52
















              Worked for me using Cordova, by setting only the max SDK version in AndroidManifest.xml to 22 (instead of 23)

              – George Kagan
              Jun 6 '16 at 16:46





              Worked for me using Cordova, by setting only the max SDK version in AndroidManifest.xml to 22 (instead of 23)

              – George Kagan
              Jun 6 '16 at 16:46













              Does your app still work on Marshmallow and above?

              – intrepidis
              Jul 8 '17 at 11:52





              Does your app still work on Marshmallow and above?

              – intrepidis
              Jul 8 '17 at 11:52











              0














              http://developer.android.com/reference/android/provider/MediaStore.Audio.AudioColumns.html



              Try changing the line contentResolver.query



              //change it to the columns you need
              String columns = new StringMediaStore.Audio.AudioColumns.DATA;
              Cursor cursor = contentResolver.query(uri, columns, null, null, null);



              public static final String MEDIA_CONTENT_CONTROL



              Not for use by third-party applications due to privacy of media consumption




              http://developer.android.com/reference/android/Manifest.permission.html#MEDIA_CONTENT_CONTROL



              try to remove the <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/> first and try again?






              share|improve this answer

























              • Thanks, I tried your suggestion but it crashed on the same spot... I'm using a Mac, can that be a problem?

                – Slim Sim
                Sep 7 '15 at 14:39











              • updated, try to remove <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/> and try? It may create strange behaviour because your app request for an permission not allowed

                – Derek Fung
                Sep 7 '15 at 15:25











              • thanks, i tried to remove that permission, but it crashes at the same spot with the same error...

                – Slim Sim
                Sep 8 '15 at 5:11
















              0














              http://developer.android.com/reference/android/provider/MediaStore.Audio.AudioColumns.html



              Try changing the line contentResolver.query



              //change it to the columns you need
              String columns = new StringMediaStore.Audio.AudioColumns.DATA;
              Cursor cursor = contentResolver.query(uri, columns, null, null, null);



              public static final String MEDIA_CONTENT_CONTROL



              Not for use by third-party applications due to privacy of media consumption




              http://developer.android.com/reference/android/Manifest.permission.html#MEDIA_CONTENT_CONTROL



              try to remove the <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/> first and try again?






              share|improve this answer

























              • Thanks, I tried your suggestion but it crashed on the same spot... I'm using a Mac, can that be a problem?

                – Slim Sim
                Sep 7 '15 at 14:39











              • updated, try to remove <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/> and try? It may create strange behaviour because your app request for an permission not allowed

                – Derek Fung
                Sep 7 '15 at 15:25











              • thanks, i tried to remove that permission, but it crashes at the same spot with the same error...

                – Slim Sim
                Sep 8 '15 at 5:11














              0












              0








              0







              http://developer.android.com/reference/android/provider/MediaStore.Audio.AudioColumns.html



              Try changing the line contentResolver.query



              //change it to the columns you need
              String columns = new StringMediaStore.Audio.AudioColumns.DATA;
              Cursor cursor = contentResolver.query(uri, columns, null, null, null);



              public static final String MEDIA_CONTENT_CONTROL



              Not for use by third-party applications due to privacy of media consumption




              http://developer.android.com/reference/android/Manifest.permission.html#MEDIA_CONTENT_CONTROL



              try to remove the <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/> first and try again?






              share|improve this answer















              http://developer.android.com/reference/android/provider/MediaStore.Audio.AudioColumns.html



              Try changing the line contentResolver.query



              //change it to the columns you need
              String columns = new StringMediaStore.Audio.AudioColumns.DATA;
              Cursor cursor = contentResolver.query(uri, columns, null, null, null);



              public static final String MEDIA_CONTENT_CONTROL



              Not for use by third-party applications due to privacy of media consumption




              http://developer.android.com/reference/android/Manifest.permission.html#MEDIA_CONTENT_CONTROL



              try to remove the <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/> first and try again?







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Sep 7 '15 at 15:24

























              answered Sep 7 '15 at 5:51









              Derek FungDerek Fung

              7,26411928




              7,26411928












              • Thanks, I tried your suggestion but it crashed on the same spot... I'm using a Mac, can that be a problem?

                – Slim Sim
                Sep 7 '15 at 14:39











              • updated, try to remove <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/> and try? It may create strange behaviour because your app request for an permission not allowed

                – Derek Fung
                Sep 7 '15 at 15:25











              • thanks, i tried to remove that permission, but it crashes at the same spot with the same error...

                – Slim Sim
                Sep 8 '15 at 5:11


















              • Thanks, I tried your suggestion but it crashed on the same spot... I'm using a Mac, can that be a problem?

                – Slim Sim
                Sep 7 '15 at 14:39











              • updated, try to remove <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/> and try? It may create strange behaviour because your app request for an permission not allowed

                – Derek Fung
                Sep 7 '15 at 15:25











              • thanks, i tried to remove that permission, but it crashes at the same spot with the same error...

                – Slim Sim
                Sep 8 '15 at 5:11

















              Thanks, I tried your suggestion but it crashed on the same spot... I'm using a Mac, can that be a problem?

              – Slim Sim
              Sep 7 '15 at 14:39





              Thanks, I tried your suggestion but it crashed on the same spot... I'm using a Mac, can that be a problem?

              – Slim Sim
              Sep 7 '15 at 14:39













              updated, try to remove <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/> and try? It may create strange behaviour because your app request for an permission not allowed

              – Derek Fung
              Sep 7 '15 at 15:25





              updated, try to remove <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/> and try? It may create strange behaviour because your app request for an permission not allowed

              – Derek Fung
              Sep 7 '15 at 15:25













              thanks, i tried to remove that permission, but it crashes at the same spot with the same error...

              – Slim Sim
              Sep 8 '15 at 5:11






              thanks, i tried to remove that permission, but it crashes at the same spot with the same error...

              – Slim Sim
              Sep 8 '15 at 5:11












              0














              Please Check below code that using that You can find all Music Files from sdcard :



              public class MainActivity extends Activity

              @Override
              protected void onCreate(Bundle savedInstanceState)
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_animations);
              getAllSongsFromSDCARD();



              public void getAllSongsFromSDCARD()
              String STAR = "*" ;
              Cursor cursor;
              Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
              String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

              cursor = managedQuery(allsongsuri, STAR, selection, null, null);

              if (cursor != null)
              if (cursor.moveToFirst())
              do
              String song_name = cursor
              .getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
              int song_id = cursor.getInt(cursor
              .getColumnIndex(MediaStore.Audio.Media._ID));

              String fullpath = cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.DATA));

              String album_name = cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.ALBUM));
              int album_id = cursor.getInt(cursor
              .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

              String artist_name = cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.ARTIST));
              int artist_id = cursor.getInt(cursor
              .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
              System.out.println("sonng name"+fullpath);
              while (cursor.moveToNext());


              cursor.close();







              I have also added following line in the AndroidManifest.xml file as below:



              <uses-sdk
              android:minSdkVersion="16"
              android:targetSdkVersion="17" />

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





              share|improve this answer

























              • Thanks, I have tried something similar before, but I tried your code and it crashes on "cursor = managedQuery(allsongsuri, STAR, selection, null, null);", same error as before. By the way; managedQuery is deprecated, but it still crashes if i change it to "contentResolver.query", which have the same input parameters...

                – Slim Sim
                Sep 7 '15 at 14:32











              • Hi I am able to get All sdcadr Songs using above code. with Android Version 4.2.2 Device and also able to with Version 5.0.2. In which You are trying. What Error you got please describe with Android Version

                – Rajan Bhavsar
                Sep 8 '15 at 4:19











              • I have updated the question with the stack-trace, hope that helps. What do you mean with Android Version? (I'm using apk 23)

                – Slim Sim
                Sep 8 '15 at 5:18











              • Ok Let me Give Full Source code and Check Out that again.

                – Rajan Bhavsar
                Sep 8 '15 at 5:56











              • if you want to test for yourself, download the zip here: drive.google.com/open?id=0B2AV8VRk9HUeVzFXSWVRZU9TRUk when you run it, press the left initialize-button, to generate the crach.

                – Slim Sim
                Sep 8 '15 at 15:42
















              0














              Please Check below code that using that You can find all Music Files from sdcard :



              public class MainActivity extends Activity

              @Override
              protected void onCreate(Bundle savedInstanceState)
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_animations);
              getAllSongsFromSDCARD();



              public void getAllSongsFromSDCARD()
              String STAR = "*" ;
              Cursor cursor;
              Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
              String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

              cursor = managedQuery(allsongsuri, STAR, selection, null, null);

              if (cursor != null)
              if (cursor.moveToFirst())
              do
              String song_name = cursor
              .getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
              int song_id = cursor.getInt(cursor
              .getColumnIndex(MediaStore.Audio.Media._ID));

              String fullpath = cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.DATA));

              String album_name = cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.ALBUM));
              int album_id = cursor.getInt(cursor
              .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

              String artist_name = cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.ARTIST));
              int artist_id = cursor.getInt(cursor
              .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
              System.out.println("sonng name"+fullpath);
              while (cursor.moveToNext());


              cursor.close();







              I have also added following line in the AndroidManifest.xml file as below:



              <uses-sdk
              android:minSdkVersion="16"
              android:targetSdkVersion="17" />

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





              share|improve this answer

























              • Thanks, I have tried something similar before, but I tried your code and it crashes on "cursor = managedQuery(allsongsuri, STAR, selection, null, null);", same error as before. By the way; managedQuery is deprecated, but it still crashes if i change it to "contentResolver.query", which have the same input parameters...

                – Slim Sim
                Sep 7 '15 at 14:32











              • Hi I am able to get All sdcadr Songs using above code. with Android Version 4.2.2 Device and also able to with Version 5.0.2. In which You are trying. What Error you got please describe with Android Version

                – Rajan Bhavsar
                Sep 8 '15 at 4:19











              • I have updated the question with the stack-trace, hope that helps. What do you mean with Android Version? (I'm using apk 23)

                – Slim Sim
                Sep 8 '15 at 5:18











              • Ok Let me Give Full Source code and Check Out that again.

                – Rajan Bhavsar
                Sep 8 '15 at 5:56











              • if you want to test for yourself, download the zip here: drive.google.com/open?id=0B2AV8VRk9HUeVzFXSWVRZU9TRUk when you run it, press the left initialize-button, to generate the crach.

                – Slim Sim
                Sep 8 '15 at 15:42














              0












              0








              0







              Please Check below code that using that You can find all Music Files from sdcard :



              public class MainActivity extends Activity

              @Override
              protected void onCreate(Bundle savedInstanceState)
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_animations);
              getAllSongsFromSDCARD();



              public void getAllSongsFromSDCARD()
              String STAR = "*" ;
              Cursor cursor;
              Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
              String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

              cursor = managedQuery(allsongsuri, STAR, selection, null, null);

              if (cursor != null)
              if (cursor.moveToFirst())
              do
              String song_name = cursor
              .getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
              int song_id = cursor.getInt(cursor
              .getColumnIndex(MediaStore.Audio.Media._ID));

              String fullpath = cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.DATA));

              String album_name = cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.ALBUM));
              int album_id = cursor.getInt(cursor
              .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

              String artist_name = cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.ARTIST));
              int artist_id = cursor.getInt(cursor
              .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
              System.out.println("sonng name"+fullpath);
              while (cursor.moveToNext());


              cursor.close();







              I have also added following line in the AndroidManifest.xml file as below:



              <uses-sdk
              android:minSdkVersion="16"
              android:targetSdkVersion="17" />

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





              share|improve this answer















              Please Check below code that using that You can find all Music Files from sdcard :



              public class MainActivity extends Activity

              @Override
              protected void onCreate(Bundle savedInstanceState)
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_animations);
              getAllSongsFromSDCARD();



              public void getAllSongsFromSDCARD()
              String STAR = "*" ;
              Cursor cursor;
              Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
              String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

              cursor = managedQuery(allsongsuri, STAR, selection, null, null);

              if (cursor != null)
              if (cursor.moveToFirst())
              do
              String song_name = cursor
              .getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
              int song_id = cursor.getInt(cursor
              .getColumnIndex(MediaStore.Audio.Media._ID));

              String fullpath = cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.DATA));

              String album_name = cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.ALBUM));
              int album_id = cursor.getInt(cursor
              .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

              String artist_name = cursor.getString(cursor
              .getColumnIndex(MediaStore.Audio.Media.ARTIST));
              int artist_id = cursor.getInt(cursor
              .getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
              System.out.println("sonng name"+fullpath);
              while (cursor.moveToNext());


              cursor.close();







              I have also added following line in the AndroidManifest.xml file as below:



              <uses-sdk
              android:minSdkVersion="16"
              android:targetSdkVersion="17" />

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






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Sep 8 '15 at 6:00

























              answered Sep 7 '15 at 5:56









              Rajan BhavsarRajan Bhavsar

              1,485520




              1,485520












              • Thanks, I have tried something similar before, but I tried your code and it crashes on "cursor = managedQuery(allsongsuri, STAR, selection, null, null);", same error as before. By the way; managedQuery is deprecated, but it still crashes if i change it to "contentResolver.query", which have the same input parameters...

                – Slim Sim
                Sep 7 '15 at 14:32











              • Hi I am able to get All sdcadr Songs using above code. with Android Version 4.2.2 Device and also able to with Version 5.0.2. In which You are trying. What Error you got please describe with Android Version

                – Rajan Bhavsar
                Sep 8 '15 at 4:19











              • I have updated the question with the stack-trace, hope that helps. What do you mean with Android Version? (I'm using apk 23)

                – Slim Sim
                Sep 8 '15 at 5:18











              • Ok Let me Give Full Source code and Check Out that again.

                – Rajan Bhavsar
                Sep 8 '15 at 5:56











              • if you want to test for yourself, download the zip here: drive.google.com/open?id=0B2AV8VRk9HUeVzFXSWVRZU9TRUk when you run it, press the left initialize-button, to generate the crach.

                – Slim Sim
                Sep 8 '15 at 15:42


















              • Thanks, I have tried something similar before, but I tried your code and it crashes on "cursor = managedQuery(allsongsuri, STAR, selection, null, null);", same error as before. By the way; managedQuery is deprecated, but it still crashes if i change it to "contentResolver.query", which have the same input parameters...

                – Slim Sim
                Sep 7 '15 at 14:32











              • Hi I am able to get All sdcadr Songs using above code. with Android Version 4.2.2 Device and also able to with Version 5.0.2. In which You are trying. What Error you got please describe with Android Version

                – Rajan Bhavsar
                Sep 8 '15 at 4:19











              • I have updated the question with the stack-trace, hope that helps. What do you mean with Android Version? (I'm using apk 23)

                – Slim Sim
                Sep 8 '15 at 5:18











              • Ok Let me Give Full Source code and Check Out that again.

                – Rajan Bhavsar
                Sep 8 '15 at 5:56











              • if you want to test for yourself, download the zip here: drive.google.com/open?id=0B2AV8VRk9HUeVzFXSWVRZU9TRUk when you run it, press the left initialize-button, to generate the crach.

                – Slim Sim
                Sep 8 '15 at 15:42

















              Thanks, I have tried something similar before, but I tried your code and it crashes on "cursor = managedQuery(allsongsuri, STAR, selection, null, null);", same error as before. By the way; managedQuery is deprecated, but it still crashes if i change it to "contentResolver.query", which have the same input parameters...

              – Slim Sim
              Sep 7 '15 at 14:32





              Thanks, I have tried something similar before, but I tried your code and it crashes on "cursor = managedQuery(allsongsuri, STAR, selection, null, null);", same error as before. By the way; managedQuery is deprecated, but it still crashes if i change it to "contentResolver.query", which have the same input parameters...

              – Slim Sim
              Sep 7 '15 at 14:32













              Hi I am able to get All sdcadr Songs using above code. with Android Version 4.2.2 Device and also able to with Version 5.0.2. In which You are trying. What Error you got please describe with Android Version

              – Rajan Bhavsar
              Sep 8 '15 at 4:19





              Hi I am able to get All sdcadr Songs using above code. with Android Version 4.2.2 Device and also able to with Version 5.0.2. In which You are trying. What Error you got please describe with Android Version

              – Rajan Bhavsar
              Sep 8 '15 at 4:19













              I have updated the question with the stack-trace, hope that helps. What do you mean with Android Version? (I'm using apk 23)

              – Slim Sim
              Sep 8 '15 at 5:18





              I have updated the question with the stack-trace, hope that helps. What do you mean with Android Version? (I'm using apk 23)

              – Slim Sim
              Sep 8 '15 at 5:18













              Ok Let me Give Full Source code and Check Out that again.

              – Rajan Bhavsar
              Sep 8 '15 at 5:56





              Ok Let me Give Full Source code and Check Out that again.

              – Rajan Bhavsar
              Sep 8 '15 at 5:56













              if you want to test for yourself, download the zip here: drive.google.com/open?id=0B2AV8VRk9HUeVzFXSWVRZU9TRUk when you run it, press the left initialize-button, to generate the crach.

              – Slim Sim
              Sep 8 '15 at 15:42






              if you want to test for yourself, download the zip here: drive.google.com/open?id=0B2AV8VRk9HUeVzFXSWVRZU9TRUk when you run it, press the left initialize-button, to generate the crach.

              – Slim Sim
              Sep 8 '15 at 15:42












              0














              In addition of all answers.
              You can also specify the minsdk to apply with this annotation



              @TargetApi(_apiLevel_)


              I used this in order to accept this request even my minsdk is 18. What it does is that the method only runs when device targets "_apilevel_" and upper.
              Here's my method:



               @TargetApi(23)
              void solicitarPermisos()

              if (ContextCompat.checkSelfPermission(this,permiso)
              != PackageManager.PERMISSION_GRANTED)

              // Should we show an explanation?
              if (shouldShowRequestPermissionRationale(
              Manifest.permission.READ_EXTERNAL_STORAGE))
              // Explain to the user why we need to read the contacts


              requestPermissions(new StringManifest.permission.READ_EXTERNAL_STORAGE,
              1);

              // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
              // app-defined int constant that should be quite unique

              return;








              share|improve this answer





























                0














                In addition of all answers.
                You can also specify the minsdk to apply with this annotation



                @TargetApi(_apiLevel_)


                I used this in order to accept this request even my minsdk is 18. What it does is that the method only runs when device targets "_apilevel_" and upper.
                Here's my method:



                 @TargetApi(23)
                void solicitarPermisos()

                if (ContextCompat.checkSelfPermission(this,permiso)
                != PackageManager.PERMISSION_GRANTED)

                // Should we show an explanation?
                if (shouldShowRequestPermissionRationale(
                Manifest.permission.READ_EXTERNAL_STORAGE))
                // Explain to the user why we need to read the contacts


                requestPermissions(new StringManifest.permission.READ_EXTERNAL_STORAGE,
                1);

                // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
                // app-defined int constant that should be quite unique

                return;








                share|improve this answer



























                  0












                  0








                  0







                  In addition of all answers.
                  You can also specify the minsdk to apply with this annotation



                  @TargetApi(_apiLevel_)


                  I used this in order to accept this request even my minsdk is 18. What it does is that the method only runs when device targets "_apilevel_" and upper.
                  Here's my method:



                   @TargetApi(23)
                  void solicitarPermisos()

                  if (ContextCompat.checkSelfPermission(this,permiso)
                  != PackageManager.PERMISSION_GRANTED)

                  // Should we show an explanation?
                  if (shouldShowRequestPermissionRationale(
                  Manifest.permission.READ_EXTERNAL_STORAGE))
                  // Explain to the user why we need to read the contacts


                  requestPermissions(new StringManifest.permission.READ_EXTERNAL_STORAGE,
                  1);

                  // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
                  // app-defined int constant that should be quite unique

                  return;








                  share|improve this answer















                  In addition of all answers.
                  You can also specify the minsdk to apply with this annotation



                  @TargetApi(_apiLevel_)


                  I used this in order to accept this request even my minsdk is 18. What it does is that the method only runs when device targets "_apilevel_" and upper.
                  Here's my method:



                   @TargetApi(23)
                  void solicitarPermisos()

                  if (ContextCompat.checkSelfPermission(this,permiso)
                  != PackageManager.PERMISSION_GRANTED)

                  // Should we show an explanation?
                  if (shouldShowRequestPermissionRationale(
                  Manifest.permission.READ_EXTERNAL_STORAGE))
                  // Explain to the user why we need to read the contacts


                  requestPermissions(new StringManifest.permission.READ_EXTERNAL_STORAGE,
                  1);

                  // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an
                  // app-defined int constant that should be quite unique

                  return;









                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 27 '17 at 19:20

























                  answered Sep 27 '17 at 13:42









                  Gian GomenGian Gomen

                  676158




                  676158



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32431723%2fread-external-storage-permission-for-android%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo