Android AsyncTask,如何获得结果?

时间:2015-07-09 12:55:22

标签: android android-asynctask

我需要在我的应用中从网上加载图片,我在这里找到了一个很好的例子,但我无法弄清楚如何在我的主要活动中使用返回的位图:

班级:

public class GetImageFromServer extends AsyncTask<Void, Void, Bitmap {
    private String sURL;

    GetImageFromServer(String urlParam) {
        sURL = urlParam;
    }

    @Override
    protected Bitmap doInBackground(Void... urlParam) {

        Bitmap bmp = null;

        //ImageView img = (ImageView) findViewById(R.id.imageView1);
        try {
            URL url = new URL(sURL);
            bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        } catch (Exception ex) {
            Log.println(1, "Profile:getImg", ex.getMessage());
        }
        return bmp;
    }

    @Override
    protected void onPreExecute() {}
}

MainActivity代码:

String urlImage = "http://www.xxxxxx.com/css/images/xxxxxx.png";
GetImageFromServer gifs = new GetImageFromServer(urlImage);
gifs.execute();

if(person.has("Avatar")) {Avatar.setImageBitmap( gifs.execute())}

错误是:
    gifs.execute()

感谢您的帮助!

添加:

我添加了这个“cancel(true)”,因为我在几次启动/调试/关闭后遇到了JSON webservices的连接问题,但我似乎没有工作:

@Override
protected Bitmap doInBackground(String... urlParam) {
    if (isCancelled())
        this.cancel(true);


    Bitmap b = null;........          

@Override
protected void onPostExecute(Bitmap result) {
    // use the result
    mImageView.setImageBitmap(result);
    this.cancel(true);
}

assynctasks会阻止我的应用程序连接到我的网络服务吗?

2 个答案:

答案 0 :(得分:1)

有一个用于执行异步图像加载的好库,这里有一个链接:https://github.com/square/picasso

或者您可以遵循这种方法:

public class LoadImageAsyncTask extends AsyncTask<String, Void, Bitmap> {

private ImageView mImageView;

    public LoadImageAsyncTask(ImageView imageView) {
        mImageView = imageView;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        Bitmap bitmap = null;
        try {
            URL url = new URL(params[0]);
            bitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());


        } catch (Exception ex) {
            Log.println(1, "Profile:getImg", ex.getMessage());
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        mImageView.setImageBitmap(bitmap);
    }
}

在您的活动电话中:

LoadImageAsyncTask task = new LoadImageAsyncTask(findViewById(R.id.yourImageId)).execute(stringUrl);

答案 1 :(得分:0)

你必须实施回调

你的asynctask

public class GetImageFromServer extends AsyncTask<Void, Void, Bitmap> {

    private String sURL;
    private Bitmap b;
    YourCallback mOwner;

    GetImageFromServer(YourCallback owner, String urlParam) {
        sURL = urlParam;
        mOwner = owner;
    }

    @Override
    protected Bitmap doInBackground(Void... urlParam) {
        try {
            URL url = new URL(sURL);
            b = BitmapFactory.decodeStream(url.openConnection()
                    .getInputStream());

        } catch (Exception ex) {
            Log.println(1, "Profile:getImg", ex.getMessage());
        }
        return b;
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        // use the result
        super.onPostExecute(result);
        if (mOwner != null)
            mOwner.CallbackFunction(result);
    }

    public interface YourCallback {
        void CallbackFunction(Bitmap result);
    }
}

您的MainActivity

public class MainActivity  extends Activity implements YourCallback {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        GetImageFromServer gifs = new GetImageFromServer(urlImage, b);
        gifs.execute();
    }

    @Override
    public void CallbackFunction(Bitmap result) {
        if (person.has("Avatar")) {
            Avatar.setImageBitmap(result);
        }

    }

}