How to return a Bitmap from an Async Task in Android app(如何从 Android 应用程序中的异步任务返回位图)
问题描述
好的,所以这段代码就在 Android 开发者网站上,它将 ImageView
设置为 Bitmap
:
Ok, so this code is right off the Android Developer site which sets an ImageView
to a Bitmap
:
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private int data = 0;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
return decodeSampledBitmapFromResource(getResources(), data, 100, 100));
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
基本上,我想做的不是做
And basically, what I want to do is instead of doing
imageView.setImageBitmap(bitmap);
我想返回那个 Bitmap
以便我可以在我的主 UI 中使用它.有什么办法可以做到这一点吗?对不起,我对 AsyncTask
缺乏了解,我最近才开始学习它.谢谢!
imageView.setImageBitmap(bitmap);
I want to return that Bitmap
so I can then use it in my Main UI. Is there any way of going about doing this? Sorry about my lack of knowledge on AsyncTask
, I just started learning it recently. Thank you!
推荐答案
其实不需要返回位图,而是在主Ui线程中使用.您可以在 PostExecute 上执行此操作,因为 postExecute 在 UI 线程中运行.你的问题??是的,你可以从 AsynTask 返回位图,你可以这样做
Actually there is no need to return bitmap and than use it in main Ui Thread. You can do this on PostExecute becuase postExecute runs in UI Thread. Your question?? Yes you can return bitmap from AsynTask you can do something like this
private class myTask extends AsyncTask<Void,Void,Bitmap>{
protected Bitmap doInBackground(Void... params) {
//do stuff
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
//do stuff
}
}
现在你可以通过调用来获取位图
Now you can get bitmap by calling
Bitmap returned_bitmap = new myTask().execute().get()
再一次这样不好,这会挂起你的 UI.但是你可以像这样从 Aysnc 获得价值.
Again this is not good this will hang your UI. But you can get value from Aysnc like this.
另一种方法是使用接口实现回调
public interface MyInterface
{
public void onPostExecute();
}
活动类
public class MyActivity extends Activity implements MyInterface{
private Bitmap Image;
public void onCreate(Bundle b)
{
super.onCreate(b);
new MyTask(this).execute();
}
@Override
public void onPostExecute() {
//Now you have your bitmap update by do in background
//do stuff here
}
private class MyTask extends AsyncTask<Void, Void, Void>
{
MyInterface myinterface;
MyTask(MyInterface mi)
{
myinterface = mi;
}
@Override
protected Void doInBackground(Void... params) {
//firt declare bitmap as class member of MyActivity
//update bitmap here and then call
myinterface.onPostExecute();
return null;
}
}
}
这篇关于如何从 Android 应用程序中的异步任务返回位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 Android 应用程序中的异步任务返回位图


- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01