本文介绍了回调怪异的行为(Android版,毕加索库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的毕加索库来管理我的图片上传和缓存。当我试图执行此code:

I am using Picasso library to manage my image uploading and caching. When I am trying to execute this code:

    Picasso.with(this)
        .load(AppServer.getImageUrl() + "/" + eventInfo.getImageName())
        .placeholder(R.drawable.calendar)
        .error(R.drawable.calendar)
        .into(new Target()
        {

            @Override
            public void onPrepareLoad(Drawable drawable)
            {
            }

            @Override
            public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from)
            {
                cropImage(photo); //not getting here
            }

            @Override
            public void onBitmapFailed(Drawable arg0)
            {
            }
        });

我并不是进入的int onBitmapLoaded 回调。只有当我关闭(回去)的活动,并重新打开它,我看到的图像(进入 onBitmapLoaded )。

I am not entering int the onBitmapLoaded callback. Only if I close the activity (going back) and re opening it I see the image (entering into onBitmapLoaded).

但是,如果我将只是增加一些吐司消息到上prepareLoad 回调每一件事工作正常。这里是满code:

But if I will change my code by just adding some Toast message into the onPrepareLoad callback every thing works fine. here is the full code:

    Picasso.with(this)
        .load(AppServer.getImageUrl() + "/" + eventInfo.getImageName())
        .placeholder(R.drawable.calendar)
        .error(R.drawable.calendar)
        .into(new Target()
        {

            @Override
            public void onPrepareLoad(Drawable drawable)
            {
                Toast.makeText(thisActivity, "message", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from)
            {
                cropImage(photo);
            }

            @Override
            public void onBitmapFailed(Drawable arg0)
            {
            }
        });

为什么吐司使得它的工作?有什么错呢?

Why the Toast makes it work? what is wrong with it?

推荐答案

我声明了一个目标实例作为类成员解决的问题。然后初始化它。像这样的:

I solved the issue by declaring a Target instance as class member. then initialised it. Like this:

    target = new Target()
    {

        @Override
        public void onPrepareLoad(Drawable drawable)
        {
        }

        @Override
        public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from)
        {
            cropEventImage(photo);
        }

        @Override
        public void onBitmapFailed(Drawable arg0)
        {
        }
    };

    Picasso.with(this)
        .load(AppServer.getImageUrl() + "/" + eventInfo.getImageName())
        .placeholder(R.drawable.calendar)
        .error(R.drawable.calendar)
        .into(target);

这篇关于回调怪异的行为(Android版,毕加索库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:59