如何获得读取外部存储权限?
Posted
技术标签:
【中文标题】如何获得读取外部存储权限?【英文标题】:How to get read external storage permissions? 【发布时间】:2017-08-26 05:01:31 【问题描述】:我正在为 android 开发一个 mp3 播放器应用程序,但遇到与用户权限相关的错误(READ_EXTERNAL_STORAGE)
这是我请求权限的代码:
int permissionCheck = ContextCompat.checkSelfPermission(PlayListActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE);
if (ContextCompat.checkSelfPermission(PlayListActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
// OPCIONAL(explicaciones de poque pedimos los permisos)
if (ActivityCompat.shouldShowRequestPermissionRationale(PlayListActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE))
else
//pedir permisos
ActivityCompat.requestPermissions(PlayListActivity.this,
new String[]Manifest.permission.READ_EXTERNAL_STORAGE,
permissionCheck);
这是剩下的代码:
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
SongsManager plm = new SongsManager(PlayListActivity.this);
// get all songs from sdcard
this.songsList = plm.getPlayList();
// looping through playlist
for (int i = 0; i < songsList.size(); i++)
// creating new HashMap
HashMap<String, String> song = songsList.get(i);
// adding HashList to ArrayList
songsListData.add(song);
// se añaden las canciones a la ListView mediante un adapter utilizando el layout playlist_item
ListAdapter adapter = new SimpleAdapter(this, songsListData,
R.layout.playlist_item, new String[] "songTitle" , new int[]
R.id.songTitle );
setListAdapter(adapter);
这是错误:
03-31 22:09:16.159 2258-2258/com.androidchatapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.androidchatapp, PID: 2258
java.lang.RuntimeException: Unable to start activity ComponentInfocom.androidchatapp/com.androidchatapp.PlayListActivity: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2258, uid=10184 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
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=2258, uid=10184 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1620)
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:493)
at android.content.ContentResolver.query(ContentResolver.java:435)
at com.androidchatapp.SongsManager.getPlayList(SongsManager.java:48)
at com.androidchatapp.PlayListActivity.onCreate(PlayListActivity.java:65)
at android.app.Activity.performCreate(Activity.java:6323)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
at android.app.ActivityThread.access$900(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
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)
【问题讨论】:
您是否在清单文件中添加了权限?你已经从代码中请求了权限,但是你可能忘记在清单文件中添加这个权限标签
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
【讨论】:
【参考方案2】:Android从Marshmallow OS引入了runtime权限,所以我们需要用Manifest权限来实现runtime权限。如果 CompileSdkVersion = 23
您可以在屏幕打开时(在 onCreate() 中)或按钮的单击事件(Like -play Music)请求权限
使用此方法检查运行时权限是否启用
private static final int READ_STORAGE_PERMISSION_REQUEST_CODE = 41;
public boolean checkPermissionForReadExtertalStorage()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
int result = context.checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED;
return false;
如果它返回 false 然后调用下面的方法,它将显示一个对话框以获取权限
public void requestPermissionForReadExtertalStorage() throws Exception
try
ActivityCompat.requestPermissions((Activity) context, new String[]android.Manifest.permission.READ_EXTERNAL_STORAGE,
READ_STORAGE_PERMISSION_REQUEST_CODE);
catch (Exception e)
e.printStackTrace();
throw e;
【讨论】:
如果有人需要知道:static final Integer READ_STORAGE_PERMISSION_REQUEST_CODE=0x3;您可以使用任何数字。以上是关于如何获得读取外部存储权限?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Spark 结构化流中的 Cassandra 等外部存储读取 Kafka 和查询?