Android读取外部存储提供securityException

时间:2015-02-14 17:38:01

标签: android securityexception

我正在尝试获取设备中的音乐文件。这是我做的:

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

我的清单中有以下权限:

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

当我在Android版本4.3的设备上运行该代码时,它可以正常工作。但是当我在Android版本5.0.2的设备上运行它时,它会给出

  

“java.lang.SecurityException:Permission Denial:reading   com.android.providers.media.MediaProvider uri   content://来自pid = 5701的媒体/外部/音频/媒体,uid = 10054需要   android.permission.READ_EXTERNAL_STORAGE“

谁能告诉我为什么?这是完整的堆栈跟踪:

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)

感谢。

2 个答案:

答案 0 :(得分:9)

  

我的清单中有以下权限

通常,如果清单中有<uses-permission>个元素,则会收到权限错误,这是因为元素位于清单中的错误位置。他们需要成为<manifest>的直接子女,作为<application>的同伴。

  

但为什么我的android 4.3设备没有问题,但这是5.0.2设备中的一个问题?

在Android 4.4之前,READ_EXTERNAL_STORAGE要么不存在,要么在普通设备上没有强制执行。

答案 1 :(得分:2)

“CommonsWare”在其他链接上的一个非常好的答案

现在没有获得您的许可的重要原因是您的项目的targetSdkVersion为23或更高,并且您请求的权限是“危险的”。在Android 6.0中,这包括:

ACCESS_COARSE_LOCATION
ACCESS_FINE_LOCATION
ADD_VOICEMAIL
BODY_SENSORS
CALL_PHONE
CAMERA
GET_ACCOUNTS
PROCESS_OUTGOING_CALLS
功能(6)
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

对于这些权限,您的targetSdkVersion 23+应用程序不仅需要拥有$intersectArray元素,而且还必须在运行时从Android 6.0+设备上的用户请求这些权限, checkSelfPermission()和requestPermissions()等方法。

作为临时解决方法,请将targetSdkVersion降至23以下。

但是,最终,您有理由希望targetSdkVersion为23或更高。那时,您需要调整应用程序以使用新的运行时权限系统。 Android 6.0预览文档有一个专门讨论该主题的页面。

现在我将我的targetSdkVersion从23降级到21,但它是一个临时解决方案。