RemoteControlClient putbitmap()崩溃了应用程序

时间:2017-04-13 07:36:54

标签: android metadata android-music-player

在我的媒体播放器应用中,如果用户不断跳过应用程序崩溃的歌曲,则会收到以下错误

  

android.app.LoadedApk $ $ ReceiverDispatcher Args.run(LoadedApk.java:923)                                                                                                  在android.os.Handler.handleCallback(Handler.java:739)                                                                                                  在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                                  在android.os.Looper.loop(Looper.java:145)                                                                                                  在android.app.ActivityThread.main(ActivityThread.java:5951)                                                                                                  at java.lang.reflect.Method.invoke(Native Method)                                                                                                  在java.lang.reflect.Method.invoke(Method.java:372)                                                                                                  在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1388)                                                                                                  在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)                                                                                               引起:java.lang.IllegalStateException:无法包裹回收的位图                                                                                                  在android.graphics.Bitmap.checkRecycled(Bitmap.java:393)                                                                                                  在android.graphics.Bitmap.writeToParcel(Bitmap.java:1669)                                                                                                  在android.os.Parcel.writeParcelable(Parcel.java:1357)                                                                                                  在android.os.Parcel.writeValue(Parcel.java:1262)                                                                                                  在android.os.Parcel.writeArrayMapInternal(Parcel.java:638)                                                                                                  在android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)                                                                                                  在android.os.Bundle.writeToParcel(Bundle.java:1096)                                                                                                  在android.os.Parcel.writeBundle(Parcel.java:663)                                                                                                  在android.media.MediaMetadata.writeToParcel(MediaMetadata.java:408)                                                                                                  在android.media.session.ISession $ Stub $ Proxy.setMetadata(ISession.java:426)                                                                                                  在android.media.session.MediaSession.setMetadata(MediaSession.java:411)                                                                                                  在android.media.RemoteControlClient $ MetadataEditor.apply(RemoteControlClient.java:591)                                                                                                  at java.lang.reflect.Method.invoke(Native Method)                                                                                                  在java.lang.reflect.Method.invoke(Method.java:372)                                                                                                  在com.xxxxxxxxx.musicplayer.RemoteControlClientCompat $ MetadataEditorCompat.apply(RemoteControlClientCompat.java:389)

这是编写元数据信息的代码块

BitmapDrawable mAlbumArt = Myclass.getArtwork(this, mMusicPath, true);
        if (mAlbumArt.equals(null))
            mAlbumArt = (BitmapDrawable) (this.getResources().getDrawable(R.drawable.sym_albumart_noimage));


mRemoteControlClientCompat.editMetadata(true)

.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, getArtistName())
                .putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, getArtistName())
                .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, getAlbumName())
                .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, getTrackName())
                .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, getDuration())
                .putLong(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER, getSongID())
                .putBitmap(
                        RemoteControlClientCompat.MetadataEditorCompat.METADATA_KEY_ARTWORK,
                        mAlbumArt.getBitmap())
                .apply();

如果我删除 的 .putBitmap( RemoteControlClientCompat.MetadataEditorCompat.METADATA_KEY_ARTWORK,                         mAlbumArt.getBitmap())这个问题正在修复。我已经尝试了所有与无法包裹回收位图相关的问题这里有什么问题?

1 个答案:

答案 0 :(得分:1)

在写入元数据之前,检查位图是否已被回收。

if(!mAlbumArt.getBitmap().isRecycled())
        mRemoteControlClientCompat.editMetadata(true)
                .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, getArtistName())
                .putString(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST, getArtistName())
                .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, getAlbumName())
                .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, getTrackName())
                .putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, getDuration())
                .putLong(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER, getSongID())
                .putBitmap(
                        RemoteControlClientCompat.MetadataEditorCompat.METADATA_KEY_ARTWORK,
                        mAlbumArt.getBitmap())
                .apply();