从mp3文件中提取图像会消耗大量时间

时间:2019-05-15 09:51:45

标签: android performance kotlin mp3 albumart

我基本上是想从存在的mp3文件中提取图像到android存储中,这里我成功地获取了这些图像而没有任何错误,但是主要的问题是执行和提取要花很多时间,例如大约2至3分钟这基本上使我的应用变得不友好,请帮助我解决此问题。

请看一下我的代码:

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 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!!.executeTransaction(object:Realm.Transaction {

                     override fun execute(realm:Realm) {
                        // increment index
                        var num = realm.where(Songdetails::class.java).max("id");
                        var nextID:Int
                        if(num == null) {
                            nextID = 1;
                        } else {
                            nextID = num.toInt() + 1;
                        }
                        var songitem = realm!!.createObject(Songdetails::class.java,nextID)
                        songitem.songname = thisTitle
                        songitem.songartist = thisArtist
                        songitem.songurl = thisurls
                        songitem.songimage = byteArray
                        //songitem.songimage = byteArray
                    }
                })
            }
            while (musicCursor.moveToNext())
        }
    }

0 个答案:

没有答案