首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >中介查询简化

中介查询简化
EN

Stack Overflow用户
提问于 2016-08-07 15:14:37
回答 2查看 726关注 0票数 2

我正在尝试使用Mediastore来获取设备中的所有音乐文件。我是异步检索所有文件,但需要6-7秒才能获得所有歌曲,因此在这段时间内,用户必须看到一个行列式进度栏。

我在查询中使用了一个查询来检索每首歌的AlbumArt

有什么方法或更好的方法来处理它吗?

代码语言:javascript
复制
public ArrayList<SongList> getSongs() {
        ContentResolver musicResolver = context.getContentResolver();
        Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

        Cursor musicCursor = musicResolver.query(musicUri, new String[] {MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media._ID
        ,MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.ALBUM,MediaStore.Audio.Media.ALBUM_ID,
                MediaStore.Audio.Media.COMPOSER,MediaStore.Audio.Media.YEAR,MediaStore.Audio.Media.TRACK
        }, null, null, MediaStore.Audio.Media.TITLE);

        if (musicCursor != null && musicCursor.moveToFirst()) {
            //int value=0;
            //get columns
            int titleColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.TITLE);
            int idColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media._ID);
            int artistColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.ARTIST);
            int pathColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.DATA);
            int albumColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.ALBUM);
            int albumIDColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.ALBUM_ID);
            int composerColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.COMPOSER);
            int yearColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.YEAR);
            int trackColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.TRACK);
            String thisAlbumArt = "";

            //add songs to list
            do {
                long thisId = musicCursor.getLong(idColumn);
                String thisTitle = musicCursor.getString(titleColumn);
                String thisArtist = musicCursor.getString(artistColumn);
                String thisPath = musicCursor.getString(pathColumn);
                String thisAlbum = musicCursor.getString(albumColumn);
                long thisalbumID = musicCursor.getLong(albumIDColumn);
                String thisComposer = musicCursor.getString(composerColumn);
                int thisyear = musicCursor.getInt(yearColumn);
                int track = musicCursor.getInt(trackColumn);

                songQueryList.add(thisTitle);
                Cursor cursor = musicResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                        new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
                        MediaStore.Audio.Albums._ID + "=?",
                        new String[]{musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))},
                        null);
                if (cursor!=null && cursor.moveToFirst()) {

                    thisAlbumArt = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                    cursor.close();
                }
                if(thisPath.endsWith(".mp3")|| thisPath.endsWith(".m4p") || thisPath.endsWith(".wav") ) {

                    songs.add(new SongList(thisId, thisTitle, thisAlbum, thisArtist, thisPath, thisAlbumArt ,thisyear,track ,thisComposer ,thisalbumID));
                }
                else{

                }
                //cursor.moveToNext();
            }
            while (musicCursor.moveToNext());
        }
        if(musicCursor!=null) {
            musicCursor.close();
        }
        //Log.e("HI"," "+musicCursor.isClosed());

        /*Collections.sort(songs, new Comparator<SongList>(){
            public int compare(SongList a, SongList b){
                return a.getTitle().compareToIgnoreCase(b.getTitle());
            }
        });*/

        return songs;

    }

谢谢你在高级

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-08-15 13:10:13

我找到了solution.Actually,这几行代码导致了我在do-while循环中调用的延迟-

代码语言:javascript
复制
Cursor cursor = musicResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                    new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
                    MediaStore.Audio.Albums._ID + "=?",
                    new String[]{musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))},
                    null);

我想补充的是,查询游标是非常昂贵的,占用时间非常长,因此使用多个时间会导致延迟。

相反,我对歌曲、专辑和艺术家使用了3种不同的查询,然后使用一些逻辑将它们关联起来。

希望这能有所帮助。编码愉快。

票数 0
EN

Stack Overflow用户

发布于 2017-09-28 20:14:22

这里有两个检索方法,第一个使用专辑的ID,第二个使用歌曲的ID。

代码语言:javascript
复制
// requires WRITE_EXTERNAL_STORAGE on N+
public void loadAlbumArt(int albumId, ImageView view){
    Uri artworkUri = Uri.parse("content://media/external/audio/albumart");
    Uri path = ContentUris.withAppendedId(artworkUri, albumId);
    Glide.with(view.getContext()).load(path).into(view);
}

public static void loadAlbumArt(int songId, ImageView view){
     Uri artworkUri = Uri.parse("content://media/external/audio/media/" + songId + "/albumart");
     Glide.with(view.getContext()).loadFromMediaStore(artworkUri).into(view);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38815550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档