为什么毕加索在后台线程中运行会阻止onActivityResult? [英] Why does Picasso running in a background thread block onActivityResult?

查看:95
本文介绍了为什么毕加索在后台线程中运行会阻止onActivityResult?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在为应用程序中的照片提供共享模式,其中一种共享选项是在线加载图像叠加层.如果用户在叠加层加载完成之前共享照片,则我们将发送未修改图像的意图,以便通过startActivityForResult与其他应用共享,然后在onActivityResult中,用户将从共享视图返回到普通照片视图.

We are working on a share mode for photos in an app, and one of the share options is to load an overlay for the image online. If the user shares the photo before the overlay finishes loading we are sending an intent with the unmodified image to share to other apps with startActivityForResult, and then in onActivityResult we are returning the user to the normal photo view from the share view.

我们遇到的问题是,当用户从共享照片返回时,在加载叠加层的后台线程完成之前,不会调用onActivityResult.后台线程会突然开始阻塞UI线程是有原因的吗?

The issue we are running into is that when the user returns from sharing the photo, onActivityResult isn't called until the background thread that is loading the overlay finishes. Is there a reason that a background thread would suddenly start to block the UI thread?

这是我们在叠加视图类内部使用的代码,该类扩展了ImageView来管理此代码:

Here is the code we are using inside of our overlay view class which extends ImageView to manage this:

private void asyncLoadImage() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            loadImage();
        }
    }).start();
}

private void loadImage() {
    try {
        overlayBitmap = Picasso.with(getContext()).load(url).get();
        if (overlayBitmap != null) {
            displayBitmap();
        } else {
            displayError();
        }

    } catch (IOException ignored) {
        displayError();
    }
}

我们也尝试过使用Picasso目标,而不是创建自己的线程,并且遇到了完全相同的问题,到目前为止,唯一的解决方案是异步使用Ion(但是,同步使用Ion或尝试取消Ion请求调用startActivityForResult之前的期货会导致相同的UI线程阻塞问题).这感觉像是一个巨大的黑客,因为我们在应用程序的其他任何地方都使用了毕加索.

We have tried using Picasso targets as well instead of creating our own thread and we run into the exact same issue, the only solution so far has been to use Ion asynchronously (however, using Ion synchronously or trying to cancel the Ion request futures before we call startActivityForResult result in the same UI thread blocking issues). This feels like a huge hack since we are using Picasso everywhere else in the app.

是否有这些后台任务返回活动时会阻塞UI线程的原因?

Is there a reason why these background tasks would be blocking the UI thread when returning to the activity?

推荐答案

看看不需要的代码,您正在创建一个新线程.毕加索(Picasso)有一个可配置的执行器方法用于线程化.

Looking at your code you are creating a new thread when you don't need to. Picasso has a configurable executor method for threading.

使用毕加索加载超过100幅图像时,我遇到了一个问题,它会锁定并阻止UI线程在我身上,但这是由于每次我调用picasso来获取图像时,它都会创建一个新线程.我通过做一些研究解决了这个问题,发现在毕加索中有一个可配置的执行器方法.

I had an issue when loading over 100+ images using picasso and it would lock up and block the UI Thread on me but this was due to it creating a new Thread every time I called picasso to get an image. I solved this by doing a little research and found that within picasso there is a configurable executor method.

这是我的ImageHandler类的实现

This is my ImageHandler class implementation

public class ImageHandler {

private static Picasso instance;

public static Picasso getSharedInstance(Context context)
{
    if(instance == null)
    {
        instance = new Picasso.Builder(context).executor(Executors.newSingleThreadExecutor()).memoryCache(Cache.NONE).indicatorsEnabled(true).build();
        return instance;
    }
    else
    {
        return instance;
    }
}
}

我不知道这是您的问题,但是如果您尚未实施,那么值得尝试.
这是我使用它加载图像的方式

I don't know that this is your issue, but it would be worth a try if you haven't implemented it yet.
Here is the way I use it to load images

    ImageHandler.getSharedInstance(getApplicationContext()).load(imString).skipMemoryCache().resize(width, height).into(image, new Callback() {
        @Override
        public void onSuccess() {
            layout.setVisibility(View.VISIBLE);
        }

        @Override
        public void onError() {

        }
    });

这篇关于为什么毕加索在后台线程中运行会阻止onActivityResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆