在Android中,你可以加载UI线程外部资源 [英] In android can you load resources outside the ui thread

查看:177
本文介绍了在Android中,你可以加载UI线程外部资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拥有的是希望一个简单的问题。我想知道你是否能安全地从一个非UI线程加载资源。我在资源文件夹中讲的事情。我无法找到明确回答这个问题的任何文件。有很多在有关什么是不允许的UI线程的SDK提及。我从来没有看到任何东西,以这种方式提到加载资源。

I have what is hopefully a simple question. I want to know if you can safely load resources from a non-ui thread. I'm talking about things in the "res" folder. I just can't find any documentation that definitively answers this question. There are a lot of mentions in the SDK related to what is not allowed on the UI Thread. I never saw anything that mentioned loading resources in this way.

例如可以叫我从后台线程这code?
    位图图标= BitmapFactory.de codeResource(context.getResources(),R.drawable.icon_resource);

For example can I call this code from a background thread? Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_resource);

它是否有助于如果我删除了context.getResources()的一部分,只能做:
    位图图标= BitmapFactory.de codeResource(RES,R.drawable.icon_resource);

Does it help if I remove the context.getResources() part and only do: Bitmap icon = BitmapFactory.decodeResource(res, R.drawable.icon_resource);

推荐答案

即可!

但它在很大程度上取决于如何API的手柄这样的电话。例如。如果API需要一个环境,那么你可能有保存您的UI环境和它传递到后台线程提供它是正确的。

But it strongly depends on how the API's handle such calls. For example. if an API expects a context, then you may have to provide it the right one by saving your UI context and passing it on to the background thread.

然而,当涉及到设置UI组件,你将不得不返回到UI线程。

However, when it comes to setting UI components, you will have to return to the UI thread.

这篇关于在Android中,你可以加载UI线程外部资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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