2

目前,我正在开发一个音乐播放器应用程序,在该应用程序中,我创建了用于控制播放器的自定义通知,并在锁定屏幕上使用 remotecontrolclient 作为背景图稿,在更改曲目时我会更新通知和图稿。但是在更改时我收到此错误:

FATAL EXCEPTION: main Process: in.xyz.yst, PID: 19598
                 java.lang.IllegalStateException: Can't parcel a recycled bitmap
                 at android.graphics.Bitmap.checkRecycled(Bitmap.java:347)
                 at android.graphics.Bitmap.writeToParcel(Bitmap.java:1496)
                 at android.widget.RemoteViews$BitmapCache.writeBitmapsToParcel(RemoteViews.java:982)
                 at android.widget.RemoteViews.writeToParcel(RemoteViews.java:2707)
                 at android.widget.RemoteViews.clone(RemoteViews.java:1816)
                 at android.app.Notification.cloneInto(Notification.java:1474)
                 at android.app.Notification.clone(Notification.java:1448)
                 at android.app.NotificationManager.notify(NotificationManager.java:150)
                 at android.app.NotificationManager.notify(NotificationManager.java:124)
                 at in.catalystapp.catalyst.Artist.MusicPlayerService$setMediaplayer$1.onBitmapLoaded(MusicPlayerService.kt:292)
                 at com.squareup.picasso.TargetAction.complete(TargetAction.java:36)
                 at com.squareup.picasso.Picasso.deliverAction(Picasso.java:558)
                 at com.squareup.picasso.Picasso.complete(Picasso.java:510)
                 at com.squareup.picasso.Picasso$1.handleMessage(Picasso.java:117)
                 at android.os.Handler.dispatchMessage(Handler.java:102)
                 at android.os.Looper.loop(Looper.java:139)
                 at android.app.ActivityThread.main(ActivityThread.java:5298)
                 at java.lang.reflect.Method.invoke(Native Method)
                 at java.lang.reflect.Method.invoke(Method.java:372)
                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:950)
                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)

我的 setupMediaplayer 代码在这里:

mNotification.bigContentView.setImageViewResource(R.id.playpause, R.drawable.ic_pause_circle_outline_white_48dp)
    mNotification.contentView.setImageViewResource(R.id.playpause, R.drawable.ic_pause_circle_outline_white_48dp)
    mNotification.bigContentView.setTextViewText(R.id.track_title, mTrackItems.get(mCurrentposition).mTitle)
    mNotification.contentView.setTextViewText(R.id.track_title, mTrackItems.get(mCurrentposition).mTitle)
    mNotification.bigContentView.setTextViewText(R.id.track_label, mTrackItems.get(mCurrentposition).mLabel)
    mNotification.contentView.setTextViewText(R.id.track_label, mTrackItems.get(mCurrentposition).mLabel)
    Picasso.with(this).load(mTrackItems.get(mCurrentposition).mArtwork).into(object : Target{
        override fun onBitmapLoaded(notibitmap: Bitmap?, from: Picasso.LoadedFrom?) {
            if (!notibitmap!!.isRecycled){
                mBitmapImage = Bitmap.createBitmap(notibitmap)
                try {
                    mNotification.bigContentView.setImageViewBitmap(R.id.art_work,notibitmap)
                    mNotification.contentView.setImageViewBitmap(R.id.art_work,notibitmap)
                    mNotificationManager.notify(SharedPref.FOREGROUNDNOTICE_ID,mNotification)
                    if (UtilsFunctions.currentVersionSupportLockScreenControls()){
                        updateRemoteControlClientMetadata(mBitmapImage!!)
                    }
                } catch (el : IllegalStateException){                
                    Log.d(TAG,el.toString())
                    if (UtilsFunctions.currentVersionSupportLockScreenControls()){
                        updateRemoteControlClientMetadata(mBitmapImage!!)
                    }
                }
            }
            Log.d(TAG,"setup media player bitmap.isRecycled(): "+notibitmap.isRecycled)
        }
        override fun onBitmapFailed(errorDrawable: Drawable?) {
        }
        override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
        }
    })
    isstart = false
    val mUrl : String = mTrackItems.get(mCurrentposition).mStreamUrl+soundcloud_clientid
    Log.d(TAG,"On Media player Setmediaplayer with stream "+mUrl)
    mMedaiPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC)
    try {
        mMedaiPlayer.setDataSource(mUrl)
        mMedaiPlayer.prepareAsync()
        mMedaiPlayer.setOnPreparedListener(this)
        mMedaiPlayer.setOnCompletionListener(this)
    } catch (el : IllegalStateException){
        Log.d(TAG,"Illegalstate Exception"+el.toString())
        mMedaiPlayer.reset()
    } catch (ei : IOException){
        Log.d(TAG,"IO Exception"+ei.toString())
        mMedaiPlayer.reset()
    } catch (e : Exception){
        Log.d(TAG,"Exception"+e.toString())
        mMedaiPlayer.reset()
    }

更新 remotecontrolclient 的代码在这里:

if (myRemoteControlClient != null) {
        val editor = myRemoteControlClient!!.editMetadata(true)
        editor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,mBitmapImage)
        editor.apply()
        Log.d(TAG,"after update remote bitmap.isRecycled(): "+editor.getBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,mBitmapImage).isRecycled)
    }

我在stackoverflow上阅读了一些帖子,这说明我的位图被remotecontrolclient回收,但我跟踪位图是否被回收,所有日志都显示错误意味着我的位图没有被回收。我做错了什么以及如何解决这个问题

4

1 回答 1

0

在我的项目中,这些日志遇到了同样的异常。我的代码不同,但错误是一致的

bitmap.recycle();

经过一些使用位图进行大小修改的操作。我刚刚删除了这条线,现在一切都很好。没必要(回收)

于 2017-07-18T08:13:33.157 回答