无法包含回收的位图

时间:2016-08-05 08:09:11

标签: android bitmap android-bitmap illegalstateexception kotlin-android-extensions

目前我正在制作一个音乐播放器应用,我在其中为控制播放器创建自定义通知,并在锁定屏幕上使用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回收的,但是我保持位图的轨迹是否被回收,所有日志显示为false意味着我的位图不被回收。我在哪里做错了以及如何解决这个问题

1 个答案:

答案 0 :(得分:0)

在我的项目中,我遇到了与这些日志相同的异常。我的代码不同,但错误就行了

bitmap.recycle();

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