Picasso on Thread:目标回调从未调用过

时间:2016-06-06 09:46:25

标签: android multithreading picasso

我试图在Java线程中使用Picasso(我知道没有必要在线程中运行Picasso,但我的架构一度需要它)。

这是我的代码:

    @Override
    public void run() {
        Log.d(LOG_TAG, "Run " + source);
        final Target target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                Log.d(LOG_TAG, "onBitmapLoaded");
                final Bitmap bm = bitmap;
                mMainThread.post(new Runnable() {
                    @Override
                    public void run() {
                        callback.onImageDownloaded(bm);
                    }
                });
            }

            @Override
            public void onBitmapFailed(final Drawable errorDrawable) {
                Log.d(LOG_TAG, "FAILED");
            }

            @Override
            public void onPrepareLoad(final Drawable placeHolderDrawable) {
                Log.d(LOG_TAG, "Prepare Load");
            }
        };
        Picasso.with(context).load(source).resize(width, height).into(target);
    }

使用Android ThreadPoolExecutor执行运行功能。调用该函数,但不会调用任何回调(onBitmapLoadedonBitmapFailedonPrepareLoad)。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

使用以下代码段:

 Picasso.with(getContext()).load(imageUrl).into(new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        //Add your own logic
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
      //Add your own logic
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
      //Add your own logic                             
    }
});