READ_EXTERNAL_STORAGE permission for Android

I am trying to access media files (music) on a user's device in order to play them; easy application "hello world" - music player.

I followed some tutorials and they basically give the same code. But that will not work; he continues to crumble and tell 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 tried:

To put this in different places in the manifest file:

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

To add android: maxSdkVersion when reading external storage:

 <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 assign grantUriPremission between uri and cursro in javamethod:

 grantUriPermission(null, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION); 

To use this, it will not crash, but the cursor will become empty:

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

To use the INTERNAL uri content, this works as expected, but it only gives “OS sounds” such as shutter sound, low battery sound, button press, etc:

 uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI; 

Help, this should not be a difficult problem that I know, but I feel so beginner!

I read and tried (or found them inapplicable to my problem):

  • Not allowed Android READ_EXTERNAL_STORAGE
  • Require permission only for older versions of Android: maxSdkVersion not working?
  • Get file name and path from URI from media star
  • Android KitKat securityException while trying to read from MediaStore
  • Android: java.lang.SecurityException: Disclaimer: start execution

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 
+51
java android android-externalstorage music-player
07 Sep '15 at 5:36
source share
9 answers

You have two solutions to your problem. Fast is to lower targetApi to 22 (file build.gradle). Secondly, you should use a new and wonderful query model to resolve:

 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 String[]{Manifest.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; } 

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

+49
Sep 16 '15 at 19:57
source share

You request permission at runtime:

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

And in the callback below you can access the repository without any problems.

 @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. } } } 
+8
Mar 01 '16 at 10:11
source share

Is your issue fixed? What is your target SDK? Try adding android;maxSDKVersion="21" to <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

+2
Jan 27 '16 at 3:16
source share

I also had a similar error log, and here is what I did -

  • In the onCreate method, we request a dialog box for checking permissions

     ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1); 
  • Result Verification Method

     @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 } } 

White papers Request runtime permissions

+2
Dec 04 '16 at
source share

Step1: add permission to 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 String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_MEDIA); } else { readDataExternal(); } 

Step 3: override the onRequestPermissionsResult method to receive the 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 a method of retrieving data from external storage.

Thank.

+1
Jul 28 '16 at 11:30
source share

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 String[]{MediaStore.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 media privacy

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

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

0
Sep 07 '15 at 5:51 on
source share

Please check below code using this. You can find all the 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 also added the following line in the AndroidManifest.xml file, as shown 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" /> 
0
Sep 07 '15 at 5:56
source share

In addition to the answers. You can also specify minsdk for use with this annotation.

 @TargetApi(_apiLevel_) 

I used this to accept this request, even my minsdk is 18. What it does is that the method only works when the device is targeting "_apilevel_" and upper. Here is 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 String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); // MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE is an // app-defined int constant that should be quite unique return; } } 
0
Sep 27 '17 at 13:42 on
source share

Just go to your "settings", in the "device" section, select "applications", and then scroll to your MY_APPLICATION and grant the "storage" permission.

-one
Nov 30 '16 at 13:18
source share



All Articles