5

我有与 RemoteControlClient 交互的音乐播放器应用程序。我需要加载专辑封面图像以在锁定屏幕小部件中显示它。我尝试使用 Piccasso 来实现这一点。我写了以下代码:

private final Target artworkTarget = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
        remoteControlClient.editMetadata(false).putBitmap(RemoteControlClientCompat.MetadataEditorCompat.METADATA_KEY_ARTWORK,
                bitmap).apply();
    }

    @Override
    public void onBitmapFailed(Drawable drawable) {
        Log.e(Constants.LOG_TAG, "Artwork loading failed");
    }

    @Override
    public void onPrepareLoad(Drawable drawable) {
    }
};

...

private void playNextSong(int songPosition) {
    ...

    String artworkUrl = Constants.Urls.BASE_ARTWORK_URL + currentSong.getArtworkId();
    Picasso.with(this).load(artworkUrl).skipMemoryCache().into(artworkTarget);
    // Update the remote controls
    remoteControlClient.editMetadata(true)
                .putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, currentSong.getArtist().getName())
                .putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, currentSong.getAlbum().getName())
                .putString(MediaMetadataRetriever.METADATA_KEY_TITLE, currentSong.getName())
                .apply();

   ...
   }

但有时我会遇到异常:

    ERROR/AndroidRuntime(31121): FATAL EXCEPTION: main
    java.lang.IllegalArgumentException: Cannot draw recycled bitmaps
    at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:772)
    at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:105)
    at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:440)
    at com.squareup.picasso.PicassoDrawable.draw(PicassoDrawable.java:96)
    at android.widget.ImageView.onDraw(ImageView.java:1025)
    at android.view.View.draw(View.java:13944)
    at android.view.View.getDisplayList(View.java:12838)
    at android.view.View.getDisplayList(View.java:12880)
    at android.view.View.draw(View.java:13657)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
    at android.view.View.getDisplayList(View.java:12833)
    at android.view.View.getDisplayList(View.java:12880)
    at android.view.View.draw(View.java:13657)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
    at android.widget.ListView.drawChild(ListView.java:3331)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
    at android.widget.AbsListView.dispatchDraw(AbsListView.java:2357)
    at android.widget.ListView.dispatchDraw(ListView.java:3326)
    at android.view.View.draw(View.java:13947)
    at android.widget.AbsListView.draw(AbsListView.java:3693)
    at android.view.View.getDisplayList(View.java:12838)
    at android.view.View.getDisplayList(View.java:12880)
    at android.view.View.draw(View.java:13657)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
    at android.view.View.getDisplayList(View.java:12833)
    at android.view.View.getDisplayList(View.java:12880)
    at android.view.View.draw(View.java:13657)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3083)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2920)
    at android.view.View.getDisplayList(View.java:12833)
    at android.view.View.getDisplayList(View.java:12880)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3057)
    at android.view.View.getDisplayList(View.java:12775)
    at android.view.View.getDisplayList(View.java:12880)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3057)
    at android.view.View.getDisplayList(View.java:12775)
    at android.view.View.getDisplayList(View.java:12880)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3057)
    at android.view.View.getDisplayList(View.java:12775)
    at android.view.View.getDisplayList(View.java:12880)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3057)
    at android.view.View.getDisplayList(View.java:12775)
    at android.view.View.getDisplayList(View.java:12880)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3057)
    at android.view.View.getDisplayList(View.java:12775)
    at android.view.View.getDisplayList(View.java:12880)
    at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1411)
    at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1359)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2367)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2239)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1872)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    at android.view.Choreographer.doFrame(Choreographer.java:532)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:

我试图在我的服务中保存加载的位图参考,但它没有帮助。
有人知道我的代码有什么问题吗?
提前致谢。

4

4 回答 4

11

我猜您使用的是 Android 4.3 或更高版本的设备。从 4.3 开始的 RemoteControlClient 实现在某些条件下对艺术品进行回收。看

http://androidxref.com/4.3_r2.1/xref/frameworks/base/media/java/android/media/RemoteControlClient.java#556

如果 Build.VERSION.SDK_INT >= 18,您基本上必须复制位图。

于 2013-12-10T16:41:46.210 回答
2

在您的onDestory()onDestroyView()回调中尝试调用Picasso.with(Context).cancelRequest(Target);

感谢这里的人:https ://github.com/square/picasso/issues/38

于 2013-10-25T01:46:10.327 回答
0

对我来说,解决方案是为现有的 RemoteControlClient 调用 unregisterRemoteControlClient,并在每次播放新歌曲时创建一个新的。当用户尝试使用相同的封面图片播放两次相同的歌曲时,就会出现问题。

解决问题的片段是:

if(mRemoteControlClientCompat!=null){
    RemoteControlHelper.unregisterRemoteControlClient(mAudioManager, mRemoteControlClientCompat);
}

Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.setComponent(mMediaButtonReceiverComponent);
mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(PlayerService.this, 0, intent, 0));
RemoteControlHelper.registerRemoteControlClient(mAudioManager, mRemoteControlClientCompat);
于 2014-02-19T12:23:14.787 回答
0

只是尝试在单独的线程中运行你的代码,它会工作

 new Handler().post(new Runnable() {
                          @Override
                          public void run() {
                             /// here is your code
                              bitmap_user=transform(bitmap);
                              imageButton.setImageBitmap(bitmap_user);

                          }
                      });
于 2019-08-08T07:11:25.697 回答