我試圖找到存儲中的所有mp3文件,但是我的應用崩潰了,我使用了logcat來查看原因並提供訪問外部存儲的權限問題,但我已經將清單許可和我不不爲什麼不斷崩潰。 第一個錯誤:許可訪問外部存儲的問題android
03-06 14:55:24.553: E/dex2oat(2796): Failed to create oat file: /data/dalvik-cache/x86/[email protected]@[email protected][email protected]: Permission denied
和多個錯誤:
03-06 14:55:25.274: E/DatabaseUtils(1873): Writing exception to parcel 03-06 14:55:25.274: E/DatabaseUtils(1873): java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=2783, uid=10057 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() 03-06 14:55:25.274: E/DatabaseUtils(1873): at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605) 03-06 14:55:25.274: E/DatabaseUtils(1873): at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480) 03-06 14:55:25.274: E/DatabaseUtils(1873): at android.content.ContentProvider$Transport.query(ContentProvider.java:211) 03-06 14:55:25.274: E/DatabaseUtils(1873): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112) 03-06 14:55:25.274: E/DatabaseUtils(1873): at android.os.Binder.execTransact(Binder.java:453)
我MainActivity.java
:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SongsManager songsManager = new SongsManager();
songsManager.getSongs(this);
ArrayList<Song> songList = songsManager.getSongsList();
ArrayAdapter<Song> adapterSongList = new ArrayAdapter<Song>(this, android.R.layout.simple_list_item_1, songList);
ListView listView = (ListView) findViewById(R.id.musicList);
listView.setAdapter(adapterSongList);
}
}
我SongsManager.java
:
public class SongsManager {
private ArrayList<Song> songsList;
public ArrayList<Song> getSongsList(){
return songsList;
}
public void getSongs(Context ctx){
ContentResolver musicResolver = ctx.getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if(musicCursor!=null && musicCursor.moveToFirst()){
//get columns
int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int pathConlumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String thisPath = musicCursor.getString(pathConlumn);
songsList.add(new Song(thisId, thisTitle, thisArtist, thisPath));
}
while (musicCursor.moveToNext());
}
}
}
我manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="felipe.cursoandroid.com.musicbox">
<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:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我們你的android版本是什麼? –
[Android 6.0權限錯誤]的可能重複(http://stackoverflow.com/questions/33078003/android-6-0-permission-error) – user5599807