Android external storage for reading gives securityException

I am trying to get music files in a device. that's what I'm doing:

public ArrayList<Track> getAllSongsFromSDCARD() { allTracks = new ArrayList<Track>(); String[] STAR = { "*" }; Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; Cursor cursor = mContext.getApplicationContext().getContentResolver().query(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)); String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)); int[] splitTime = null; String hours, minutes, seconds, formattedTime; try{ splitTime = splitToComponentTimes(BigDecimal.valueOf(Long.valueOf(duration)/1000)); hours = String.valueOf(splitTime[0]); minutes = String.valueOf(splitTime[1]); seconds = String.valueOf(splitTime[2]); if(hours.equals("0")){ formattedTime = minutes + ":" + seconds; }else{ formattedTime = hours + ":" + minutes + ":" + seconds; } }catch (Exception e){ formattedTime = "00:00"; } allTracks.add(new Track(song_name, song_id, fullpath, album_name, album_id, artist_name, artist_id, formattedTime)); } while (cursor.moveToNext()); } sortTracksAlphabetically(allTracks); cursor.close(); } return allTracks; } 

And I have the following permissions in my manifest:

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

When I run this code on a device with Android version 4.3, it works fine. But when I run it on a device with Android version 5.0.2, it gives

"java.lang.SecurityException: permission denied: reading com.android.providers.media.MediaProvider uri content: // media / external / audio / media from pid = 5701, uid = 10054 requires android.permission.READ_EXTERNAL_STORAGE"

Can someone tell me why? Here is the full stacktrace:

 02-14 19:25:52.812 5701-5701/com.yrazlik.musicplayer E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.yrazlik.musicplayer, PID: 5701 java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=5701, uid=10054 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.os.Parcel.readException(Parcel.java:1540) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137) at android.content.ContentProviderProxy.query(ContentProviderNative.java:420) at android.content.ContentResolver.query(ContentResolver.java:478) at android.content.ContentResolver.query(ContentResolver.java:422) at com.yrazlik.musicplayer.dialogs.AllSongsDialog.getAllSongsFromSDCARD(AllSongsDialog.java:74) at com.yrazlik.musicplayer.dialogs.AllSongsDialog.onCreate(AllSongsDialog.java:59) at android.app.Dialog.dispatchOnCreate(Dialog.java:373) at android.app.Dialog.show(Dialog.java:274) at com.yrazlik.musicplayer.fragments.PlaylistFragment$1.onClick(PlaylistFragment.java:86) at android.view.View.performClick(View.java:4756) at android.view.View$PerformClick.run(View.java:19749) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

Thanks.

+7
android securityexception
source share
2 answers

And I have the following permissions in my manifest

Often, if you get permission errors, despite the presence of <uses-permission> elements in the manifest, this is because the elements are in the wrong place on the manifest. They must be immediate children of <manifest> , like peer-to-peer <application> pairs.

But why there were no problems with my Android 4.3 device, but is this a problem in the 5.0.2 device?

Prior to Android 4.4, READ_EXTERNAL_STORAGE either did not exist or did not execute on regular devices.

+9
source share

Very good answer from "CommonsWare" to another link

The big reason not to get your permission right now is that your project has targetSdkVersion 23 or higher, and the permission you are requesting is "dangerous." In Android 6.0, this includes:

ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
ADD_VOICEMAIL
BODY_SENSORS
CALL_PHONE
CAMERA
GET_ACCOUNTS
PROCESS_OUTGOING_CALLS
READ_CALENDAR
READ_CALL_LOG
READ_CELL_BROADCASTS
READ_CONTACTS
READ_EXTERNAL_STORAGE
READ_PHONE_STATE
READ_SMS
RECEIVE_MMS
RECEIVE_SMS
RECEIVE_WAP_PUSH
RECORD_AUDIO
SEND_SMS
USE_SIP
WRITE_CALENDAR
WRITE_CALL_LOG WRITE_CONTACTS
WRITE_EXTERNAL_STORAGE

For these permissions, your targetSdkVersion 23+ application requires not only the <uses-permission> element, but you must also request these permissions at run time from the user on Android 6.0+ devices using methods such as checkSelfPermission () and requestPermissions ().

As a temporary workaround, uncheck the target version of SDkVersion below 23.

However, in the end, you will have some reason for your TargetSdkVersion target to be 23 or higher. At that time, you will need to configure the application to use the new permission system at runtime. The Android 6.0 preview documentation contains a page dedicated to this topic.

NOW I have degraded my target SDKVersion from 23 to 21 of my work, but this is a temporary solution.

+2
source share

All Articles