我正在尝试获取一系列歌曲以填充回收者视图。我对所有其他标签进行了排序,但是对于所有歌曲,它都拒绝工作。我正在使用MediaStore.Audio.Media.TITLE
。
这是我的堆栈跟踪:
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: android.database.sqlite.SQLiteException: no such column: title (code 1): , while compiling: SELECT _id, title, artist, album FROM album_info ORDER BY title ASC
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)
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 xyz.timmo.music.SongsFragment$GetAlbums.doInBackground(SongsFragment.java:88)
at xyz.timmo.music.SongsFragment$GetAlbums.doInBackground(SongsFragment.java:71)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
我的光标方法如下:
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[]{
//MediaStore.Audio.Media._ID,
MediaStore.Audio.Albums._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Artists.ARTIST,
MediaStore.Audio.Albums.ALBUM
//MediaStore.Audio.Media.DURATION
},
null,
null,
MediaStore.Audio.Media.TITLE + " ASC"
);
if (cursor != null) {
//count = cursor.getCount();
cursor.moveToFirst();
do {
artArrayList.add(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.Albums._ID)));
songsArrayList.add(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
artistsArrayList.add(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.Artists.ARTIST)));
albumsArrayList.add(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)));
} while (cursor.moveToNext());
cursor.close();
}
那么获取正确的字符串是什么?所有其他主题建议使用MediaStore.Audio.Media.TITLE
,但这似乎缺失。这是在最新的sdk中删除的东西吗?
答案 0 :(得分:1)
我明白了。当我应该提取MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
以下是未来参考的工作代码:
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{
MediaStore.Audio.Media._ID,
//MediaStore.Audio.Albums._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM
//MediaStore.Audio.Media.DURATION
},
null,
null,
MediaStore.Audio.Media.TITLE + " ASC"
);
if (cursor != null) {
//count = cursor.getCount();
cursor.moveToFirst();
do {
artArrayList.add(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.Albums._ID)));
songsArrayList.add(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
artistsArrayList.add(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.Artists.ARTIST)));
albumsArrayList.add(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM)));
} while (cursor.moveToNext());
cursor.close();