简体   繁体   English

如何从AsyncTask获取位图并将其设置为图像

[英]How do I get a bitmap from AsyncTask and set it to my image

I am trying to use an AsyncTask to convert an image URL to a bitmap in order to set it to an image. 我正在尝试使用AsyncTask将图像URL转换为位图,以便将其设置为图像。 I don't understand how to get the bitmap out of the AsyncTask so that I can set it to my the images I am creating in a loop. 我不明白如何从AsyncTask中获取位图,以便可以将其设置为循环创建的图像。 I also have tried to set the bitmap to the image inside AsyncTask. 我也尝试将位图设置为AsyncTask中的图像。 My understanding is that everything must be done inside AsyncTask but I am unable to reference the images I want to set the value for. 我的理解是,所有操作都必须在AsyncTask内部完成,但是我无法引用要为其设置值的图像。

I have researched the URL to bitmap conversion and the AsyncTask but I can't figure out how to combine them to get what I want. 我已经研究了URL到位图转换和AsyncTask的问题,但是我不知道如何将它们组合起来以获得我想要的东西。 This has to be a fairly common task. 这必须是相当常见的任务。 Why is it so difficult? 为什么这么难?

How do I reference my images in AsyncTask? 如何在AsyncTask中引用我的图像?

Can I return my bitmap and then set it to the image outside of AsyncTask? 我可以返回位图,然后将其设置为AsyncTask外部的图像吗?

Is there an easier way to set a URL as an image source? 有没有更简单的方法将URL设置为图像源?

I have included what I have so far. 我已经包括了到目前为止的内容。

Thank you for your help and taking the time to read this. 感谢您的帮助,并抽出宝贵的时间阅读本文。

class BitmapTaks extends AsyncTask<String, Void, Bitmap> {

public Bitmap doInBackground(String... urls) {
        Bitmap bm = null;
        try {
            URL aURL = new URL(urls[0]);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
       } catch (IOException e) {
           Log.e("img", "Error getting bitmap", e);
       }
       return bm;
}

protected void onPostExecute(Bitmap bm) {
   //do something after execute


}
}

Simply define final ImageView variable linked to the view you want to fill with the Bitmap: 只需定义链接到要使用位图填充的视图的最终ImageView变量:

     //inside some method e.g. onCreate()
     final ImageView imageView = (ImageView) findViewById(R.id.myPrettyImage);

     new AsyncTask<String, Void, Bitmap>() {
        @Override
        protected Bitmap doInBackground(String... params) {
            /* here's your code */
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);

            imageView.setImageBitmap(bitmap);
        }
    }.execute(/*your params*/);

Or you can create ImageView field in your AsyncTask extended task: 或者,您可以在AsyncTask扩展任务中创建ImageView字段:

private class ExtendedAsyncTask extends AsyncTask<String, Void, Bitmap> {
    private ImageView[] views;

    public void ExtendedAsyncTask(ImageView[] views) {
        this.views = views;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        /* your code here */
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        for (ImageView view: views) {
            view.setImageBitmap(bitmap);
        }
    }
}

and use it: 并使用它:

ExtendedAsyncTask aTask = new ExtendedAsyncTask(new ImageView[]{myImageView1, myImageView2, myImageView3});
aTask.execute(/*some params*/);

This has to be a fairly common task. 这必须是相当常见的任务。 Why is it so difficult? 为什么这么难?

I'd really recommend you take a look into the Android Volley library . 我真的建议您看看Android Volley库 It's made to abstract away all this AsyncTask boilerplate code. 它可以抽象出所有这些AsyncTask样板代码。 Here's a very nice example of how to set an image bitmap 这是一个很好的示例,说明如何设置图像位图

ImageLoader imageLoader = AppController.getInstance().getImageLoader();
 
// If you are using normal ImageView
imageLoader.get(Const.URL_IMAGE, new ImageListener() {
 
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, "Image Load Error: " + error.getMessage());
    }
 
    @Override
    public void onResponse(ImageContainer response, boolean arg1) {
        if (response.getBitmap() != null) {
            // load image into imageview
            imageView.setImageBitmap(response.getBitmap());
        }
    }
});

You can read more from here: http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ 您可以从此处了解更多信息: http : //www.androidhive.info/2014/05/android-working-with-volley-library-1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM