Android读取外部存储提供securityException

Android读取外部存储提供securityException,第1张

概述我想在设备中获取音乐文件.这是我做的: public ArrayList<Track> getAllSongsFromSDCARD(){ allTracks = new ArrayList<Track>(); String[] STAR = { "*" }; Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT 我想在设备中获取音乐文件.这是我做的:
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://media/external/audio/media from pID=5701,uID=10054 requires
androID.permission.READ_EXTERNAL_STORAGE”

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

02-14 19:25:52.812    5701-5701/com.yrazlik.musicplayer E/AndroIDRuntime﹕ FATAL EXCEPTION: mainProcess: com.yrazlik.musicplayer,PID: 5701java.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.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)

谢谢.

解决方法

And i have the following permissions in my manifest

通常情况下,如果您获得了权限错误,尽管有< uses-permission>清单中的元素,是因为元素位于清单中的错误位置.他们需要是< manifest>的直接孩子,作为< application>的同行.

But why there was no problem with my androID 4.3 device but it is a problem in 5.0.2 device?

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

总结

以上是内存溢出为你收集整理的Android读取外部存储提供securityException全部内容,希望文章能够帮你解决Android读取外部存储提供securityException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1136527.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存