代码之家  ›  专栏  ›  技术社区  ›  Vince VD

Hashmap是否将albumid与唱片集名称链接?

  •  2
  • Vince VD  · 技术社区  · 7 年前

    我正在尝试在我的设备上显示歌曲中的专辑名称和专辑插图。 我尝试使用比较器对专辑名称进行排序,但当我对专辑名称进行排序时,艺术品与专辑名称不匹配。

    如何将albumid与匹配的艺术品链接?

    我用毕加索来展示这张专辑的艺术和。

    我从MediaStore获得了专辑艺术。音频媒体ALBUM\u ID;

    然后我尝试使用hashmap将albumid与唱片集名称链接起来。

    但我得到了这个错误;

    E/cannotexecutetask:java。lang.NullPointerException:尝试调用接口方法“BooleanAndroid”。数据库光标。空对象引用上的moveToFirst()'

    // Creating the map  "Album IDs" -> "Album Names"
        albumIdToAlbumNameMap = new HashMap<>();
    
        //This is what we'll ask of the albums
        String[] albumColumns = {
                SONG_ALBUMID,
                SONG_ALBUM,
        };
    
        // Querying the album database
        cursor = resolver.query(musicUri, albumColumns, null, null, null);
    
        // Iterating through the results and filling the map.
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
            albumIdToAlbumNameMap.put(cursor.getString(0), cursor.getString(1));
    
        cursor.close();
    
        // Map Song IDs to Album IDs
        songIdToAlbumIdMap = new HashMap<>();
    
        // For each album, we'll query the databases
        for (String albumID : albumIdToAlbumNameMap.keySet()) {
    
            Uri uri = MediaStore.Audio.Albums.getContentUri(albumID);
    
            cursor = resolver.query(uri, new String[] { SONG_ID }, null, null, null);
    
            // Iterating through the results, populating the map
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
    
                long currentSongID = cursor.getLong(cursor.getColumnIndex(SONG_ID));
    
                songIdToAlbumIdMap.put(Long.toString(currentSongID), albumID);
            }
            cursor.close();
        }
    
    
    final String musicsOnly = MediaStore.Audio.Media.IS_MUSIC + "=1";
    
        // Actually querying the system
        cursor = resolver.query(musicUri, columns, musicsOnly, null, null);
    
        if (cursor != null && cursor.moveToFirst())
        {
            // NOTE: I tried to use MediaMetadataRetriever, but it was too slow.
            //       Even with 10 songs, it took like 13 seconds,
    
            do {
                // Creating a song from the values on the row
                QuerySongs song = new QuerySongs(cursor.getInt(cursor.getColumnIndex(SONG_ID)),
                        cursor.getString(cursor.getColumnIndex(SONG_FILEPATH)));
    
                song.setTitle      (cursor.getString(cursor.getColumnIndex(SONG_TITLE)));
                song.setArtist     (cursor.getString(cursor.getColumnIndex(SONG_ARTIST)));
                song.setAlbumID    (cursor.getInt(cursor.getColumnIndexOrThrow(SONG_ALBUMID)));
    
                // Using the previously created genre maps
                // to fill the current song genre.
                String currentGenreID   = songIdToGenreIdMap.get(Long.toString(song.getId()));
                String currentGenreName = genreIdToGenreNameMap.get(currentGenreID);
                String currentAlbumID = songIdToAlbumIdMap.get(Long.toString(song.getId()));
                String currentAlbumName = albumIdToAlbumNameMap.get(currentAlbumID);
                song.setGenre(currentGenreName);
                song.setAlbum(currentAlbumName);
    
                // Adding the song to the global list
                songs.add(song);
            }
            while (cursor.moveToNext());
        }
        else
        {
            // What do I do if I can't find any songs?
    
        }
        cursor.close();
    

    此处是否出现错误

      // Iterating through the results, populating the map
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
    
                long currentSongID = cursor.getLong(cursor.getColumnIndex(SONG_ID));
    
                songIdToAlbumIdMap.put(Long.toString(currentSongID), albumID);
            }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Xiaofeng    7 年前

    似乎需要修复查询输入以避免游标中出现null。