错误:获取媒体文件时没有这样的coloumn

时间:2018-02-04 05:30:48

标签: java android sqlite android-sqlite

在我的音乐播放器中我希望从相册中获取歌曲,但它在logs.please中给我错误。请解释这个错误为什么会发生这种情况。我在stackoverflow上看到关于这个问题的所有上一个问题但是无法理解,请帮助

 public void getAlbumsLists() {
        final Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
        _id = MediaStore.Audio.Albums._ID;
        final String album_name = MediaStore.Audio.Albums.ALBUM;
        final String artist = MediaStore.Audio.Albums.ARTIST;
        tracks = android.provider.MediaStore.Audio.Albums.NUMBER_OF_SONGS;
        final String data=MediaStore.Audio.Media.DATA;
        final String id1=MediaStore.Audio.Media._ID;
        final String tit=MediaStore.Audio.Media.TITLE;
        final String nam=MediaStore.Audio.Media.DISPLAY_NAME;
        final String typ=MediaStore.Audio.Media.MIME_TYPE;
            final String[] columns = {_id, album_name, artist, tracks,data,id1,tit,nam,typ};

            cursor = getContext().getContentResolver().query(uri, columns,null, null, null);
            if (cursor != null && cursor.moveToFirst()&&cursor.getCount()>0)
                do {
                    int x=cursor.getColumnCount();
                    System.out.println(x);
                    String id = (cursor.getString(cursor.getColumnIndex(_id)));
                    String name = cursor.getString(cursor.getColumnIndex(album_name));
                    String artist2 = cursor.getString(cursor.getColumnIndex(artist));
                    String nr = cursor.getString(cursor.getColumnIndex(tracks));
            String data1=cursor.getString(cursor.getColumnIndexOrThrow(data));
                String id2=cursor.getString(cursor.getColumnIndex(id1));
                String title=cursor.getString(cursor.getColumnIndex(tit));
                String name1=cursor.getString(cursor.getColumnIndex(nam));
                String type=cursor.getString(cursor.getColumnIndex(typ));
                    albumInfo s = new albumInfo(id, name, artist2, nr,data,id2,title,name1,type);
                    albumList.add(s);
                } while (cursor.moveToNext());

            if (cursor != null) {
                cursor.close();
            }
            recyclerView1.setAdapter(albumAdapter);
        }
  

android.database.sqlite.SQLiteException:没有这样的列:数据   (代码1):,编译时:SELECT _id,专辑,艺术家,numsongs,   duration,_id,title,_display_name,mime_type FROM album_info                                                                                   在   android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:179)                                                                                   在   android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)                                                                                   在   android.content.ContentProviderProxy.query(ContentProviderNative.java:418)                                                                                   在android.content.ContentResolver.query(ContentResolver.java:754)                                                                                   在android.content.ContentResolver.query(ContentResolver.java:704)                                                                                   在android.content.ContentResolver.query(ContentResolver.java:662)                                                                                   在layout.BlankFragment.getAlbumsLists(BlankFragment.java:154)                                                                                   在layout.BlankFragment.onCreateView(BlankFragment.java:138)

2 个答案:

答案 0 :(得分:0)

简而言之, _data 列不是MediaStore.Audio.Albums中的列,也不是其他一些列(请参阅代码)。相反,我相信他们可能来自每首曲目/歌曲。

要解决问题/问题但是只能获取ALBUM数据,您可以将代码修改为: -

public void getAlbumsLists() {
    final Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
    final String _id = MediaStore.Audio.Albums._ID;
    final String album_name = MediaStore.Audio.AlbumColumns.ALBUM;
    final String artist = MediaStore.Audio.AlbumColumns.ARTIST;
    final String tracks = android.provider.MediaStore.Audio.Albums.NUMBER_OF_SONGS;
    //final String data=MediaStore.Audio.Media.DATA; // NO
    final String id1=MediaStore.Audio.Albums._ID;
    final String tit=MediaStore.Audio.Albums.ALBUM; //NO
    final String nam=MediaStore.Audio.Albums.ALBUM_KEY; // NO
    final String typ=MediaStore.Audio.Media.MIME_TYPE; // NO
    final String art=MediaStore.Audio.Albums.ALBUM_ART; //<<<< CAN GET
    final String artstkey = MediaStore.Audio.Artists.ARTIST_KEY; //<<<< CAN GET
    final String frstyr = MediaStore.Audio.AlbumColumns.FIRST_YEAR; //<<<< CAN GET
    final String lstyr = MediaStore.Audio.AlbumColumns.LAST_YEAR; //<<<< CAN GET
    final String artstid = "artist_id"; //<<<< CAN GET

    final String[] columns = {"*"};

    Cursor cursor = this.getContentResolver().query(uri, columns,null, null, null);
    // Lists the columns in the cursor
    for (String s :cursor.getColumnNames()
         ) {
        Log.d("COLUMNS","Column = " +s);
    }
    while (cursor.moveToNext()) {
        String id = (cursor.getString(cursor.getColumnIndex(_id)));
        String name = cursor.getString(cursor.getColumnIndex(album_name));
        String artist2 = cursor.getString(cursor.getColumnIndex(artist));
        String nr = cursor.getString(cursor.getColumnIndex(tracks));
        //String data1=cursor.getString(cursor.getColumnIndexOrThrow(data)); //<<<< NOT A COLUMN
        String id2=cursor.getString(cursor.getColumnIndex(id1));
        //String title=cursor.getString(cursor.getColumnIndex(tit)); //<<<< NOT A COLUMN
        //String name1=cursor.getString(cursor.getColumnIndex(nam)); //<<<< NOT A COLUMN
        //String type=cursor.getString(cursor.getColumnIndex(typ)); //<<<< NOT A COLUMN
        // AVAIALABLE COLUMNS
        String artwork = cursor.getString(cursor.getColumnIndex(art)); //<<<< ADDED
        String artistkey = cursor.getString(cursor.getColumnIndex(artstkey)); //<<<< ADDED
        String artistid = cursor.getString(cursor.getColumnIndex(artstid)); //<<<< ADDED
        String minyear = cursor.getString(cursor.getColumnIndex(frstyr));
        String maxyear = cursor.getString(cursor.getColumnIndex(lstyr));
        albumInfo s = new albumInfo(id, name, artist2, nr,data,id2,title,name1,type); // EXCLUDED

            albumList.add(s); // EXCLUDED
        }
    cursor.close();
    recyclerView1.setAdapter(albumAdapter); // EXCLUDED
}

注释

  • 检查空值Cursor是徒劳的,没有数据的Cursor将为空(count = 0)不为空。
  • 添加了提取光标中所有列的列表
  • 添加了可用列
  • 上面已经测试了其他排除评论的行。
  • 更改了查询以获取所有行,而不是使用的列 *

    他们是: -

  • D / COLUMNS:Column = _id

  • D / COLUMNS:专栏=专辑
  • D / COLUMNS:Column = album_key
  • D / COLUMNS:Column = minyear
  • D / COLUMNS:Column = maxyear
  • D / COLUMNS:专栏=艺术家
  • D / COLUMNS:Column = artist_id
  • D / COLUMNS:Column = artist_key
  • D / COLUMNS:Column = numsongs
  • D / COLUMNS:Column = album_art

  • 最后,您需要研究如何获得每张专辑的歌曲

额外 - 获取曲目: -

以下是每张专辑的曲目(见注释): -

public void getSongsPerAlbum(String AlbumId) {
    final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    final String track_id = MediaStore.Audio.Media._ID;
    final String track_no = MediaStore.Audio.Media.TRACK;
    final String track_name = MediaStore.Audio.Media.TITLE;
    final String artist = MediaStore.Audio.Media.ARTIST;
    final String duration = MediaStore.Audio.Media.DURATION;
    final String album = MediaStore.Audio.Media.ALBUM;
    final String albumid = MediaStore.Audio.Media.ALBUM_ID;
    final String composer = MediaStore.Audio.Media.COMPOSER;
    final String year = MediaStore.Audio.Media.YEAR;
    final String path = MediaStore.Audio.Media.DATA;
    final String date_added = MediaStore.Audio.Media.DATE_ADDED;
    String[] columns = new String[]{
            track_id,
            track_no,
            track_name,
            artist,
            duration,
            album,
            albumid,
            composer,
            year,
            path,
            date_added
    };
    // Get all tracks if no album id
    Cursor csr;
    if (AlbumId == null || AlbumId.length() < 1) {
        csr = this.getContentResolver().query(uri, columns,null,null,track_no);
    } else {
        // Doesn't work think media has to be scanned first before albums are generated
        csr = this.getContentResolver().query(uri, columns,album + "=?",new String[]{AlbumId},track_no);
    }

    for (String s: csr.getColumnNames()
         ) {
        Log.d("SONGCOLUMN","Column = " + s);
    }
    while (csr.moveToNext()) {
        Log.d("SONG","Name of Song is " + csr.getString(csr.getColumnIndex(track_name)) +
                " from Album " + csr.getString(csr.getColumnIndex(album))
        );
    }
}

注释

  • 可以传递null或“”,它将获得所有曲目(测试,结果如下)
  • 上述内容再次在一项活动中进行了测试,因此将其用于上下文。您可能需要将 this 更改为 getActivity()
  • 我的外部媒体非常有限,看起来像: -

enter image description here

结果输出为: -

02-04 09:14:50.601 2799-2799/? D/SONGCOLUMN: Column = _id
02-04 09:14:50.601 2799-2799/? D/SONGCOLUMN: Column = track
02-04 09:14:50.601 2799-2799/? D/SONGCOLUMN: Column = title
02-04 09:14:50.601 2799-2799/? D/SONGCOLUMN: Column = artist
02-04 09:14:50.601 2799-2799/? D/SONGCOLUMN: Column = duration
02-04 09:14:50.601 2799-2799/? D/SONGCOLUMN: Column = album
02-04 09:14:50.601 2799-2799/? D/SONGCOLUMN: Column = album_id
02-04 09:14:50.601 2799-2799/? D/SONGCOLUMN: Column = composer
02-04 09:14:50.601 2799-2799/? D/SONGCOLUMN: Column = year
02-04 09:14:50.601 2799-2799/? D/SONGCOLUMN: Column = _data
02-04 09:14:50.601 2799-2799/? D/SONGCOLUMN: Column = date_added
02-04 09:14:50.601 2799-2799/? D/SONG: Name of Song is Ride on time from Album Music
02-04 09:14:50.601 2799-2799/? D/SONG: Name of Song is Modern Love from Album Let's Dance
02-04 09:14:50.601 2799-2799/? D/SONG: Name of Song is China Girl from Album Let's Dance
02-04 09:14:50.601 2799-2799/? D/SONG: Name of Song is Let's Dance from Album Let's Dance
02-04 09:14:50.601 2799-2799/? D/SONG: Name of Song is Without You from Album Let's Dance
02-04 09:14:50.601 2799-2799/? D/SONG: Name of Song is Ricochet from Album Let's Dance
02-04 09:14:50.601 2799-2799/? D/SONG: Name of Song is Criminal World from Album Let's Dance
02-04 09:14:50.601 2799-2799/? D/SONG: Name of Song is Cat People (Putting Out Fire) from Album Let's Dance
02-04 09:14:50.601 2799-2799/? D/SONG: Name of Song is Shake It from Album Let's Dance

答案 1 :(得分:0)

这可能太晚了,但我希望以后对某人有帮助,我遇到了同样的问题,花了一个星期的时间试图弄清楚,但实际上这很简单。 有些音乐只有一栏,在同一栏上有歌曲标题和艺术家名称,因此在使用时;

    cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)

它会显示一条错误消息,请确保您的光标已正确初始化,试图说出艺术家列不存在 所以你只需要使用;

cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME))

我希望它也能帮助别人。谢谢