我已经添加了我的AndroidManifest.xml,如下所示,但每次单击Button时都会收到SecurityException。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.root.musictest" >
<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>
按钮侦听器:
private ArrayList<String> names = new ArrayList<String>();
public void findItems(View view) {
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,null, null, null, null);
while(cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
names.add(name);
}
list.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.id.text1,
android.R.layout.simple_list_item_1,
names));
}
日志是:
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2506, uid=10083 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1465)
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:461)
at android.content.ContentResolver.query(ContentResolver.java:404)
......
我该如何解决?任何输入都会有所帮助。感谢
答案 0 :(得分:2)
替换:
<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="ANDROID.PERMISSION.WRITE_EXTERNAL_STORAGE"/>
使用:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
最重要的是这种情况 - 您正在绊倒Android Studio错误,将您的权限置于全部大写。如果您持有READ_EXTERNAL_STORAGE
,AFAIK。
WRITE_EXTERNAL_STORAGE