如何从一个异步任务在Android应用程序返回一个位图 [英] How to return a Bitmap from an Async Task in Android app

查看:81
本文介绍了如何从一个异步任务在Android应用程序返回一个位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好了,所以这code是正确的关闭它设置了一个 ImageView的Andr​​oid开发者网站位图

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(位); 我要返回位图这样我就可以再使用它我主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在UI线程运行后,可以在PostExecute做到这一点。你的问题??是的,你可以从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();
}

Activity类

Activity class

 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应用程序返回一个位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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