如何从 Android 应用程序中的异步任务返回位图

How to return a Bitmap from an Async Task in Android app(如何从 Android 应用程序中的异步任务返回位图)

本文介绍了如何从 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 应用程序中的异步任务返回位图

基础教程推荐