如何以更快的速度从mp3文件中提取专辑封面

时间:2019-05-07 09:17:48

标签: android mp3 albumart

我基本上已经创建了一个应用程序,其中我将所有mp3数据存储到Realm数据库中,所有进入字符串的数据都以非常快的速度获取和存储,但是当涉及到将图像存储并存储到字节数组中时,我的应用程序需要很长时间才能将图像获取并存储到数据库中,这最终使我的应用程序滞后了。 下面是我完成的代码,请看一下。

//此方法用于获取我遇到问题的歌曲列表。

注意:我在尝试catch块时特别遇到了问题,此处图像已成功获取,但是速度非常慢。

fun getSongList()
    {
        //query external audio
        val musicResolver = context!!.getContentResolver()
        val musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
        val musicCursor = musicResolver.query(musicUri, null, null, null, null)
        //iterate over results if valid
        if (musicCursor != null && musicCursor.moveToFirst())
        {
            //get columns
            val titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE)
            val idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID)
            val artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST)
            val url= musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA)
            //add songs to list
            do
            {
                val thisId = musicCursor.getLong(idColumn)
                val thisTitle = musicCursor.getString(titleColumn)
                val thisArtist = musicCursor.getString(artistColumn)
                val thisurls = musicCursor.getString(url)
                songList!!.add(Song(thisId, thisTitle, thisArtist,thisurls))
                var byteArray:ByteArray?=null
                try
                {
                    val retriever = MediaMetadataRetriever()
                    retriever.setDataSource(thisurls)
                    val art = retriever.getEmbeddedPicture()
                    if (art != null) {
                        var bitmap=BitmapFactory.decodeByteArray(art, 0, art.size)
                        var stream =  ByteArrayOutputStream()
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream)
                         byteArray = stream.toByteArray()
                    }
                    else
                    {
                        val icon:Bitmap = BitmapFactory.decodeResource(context!!.getResources(), R.drawable.music)
                        var stream =  ByteArrayOutputStream()
                        icon.compress(Bitmap.CompressFormat.PNG, 100, stream);
                         byteArray = stream.toByteArray()
                    }
                }catch (e: Exception){
                    e.printStackTrace()
                }
                realm!!.beginTransaction()
                var songitem = realm!!.createObject(Songdetails::class.java,Companion.getPrimaryKey())
                songitem.songname = thisTitle
                songitem.songartist = thisArtist
                songitem.songurl = thisurls
                songitem.songimage = byteArray
                realm!!.commitTransaction()
            }
            while (musicCursor.moveToNext())
        }
    }

任何建议将不胜感激!

0 个答案:

没有答案