How do I get a bitmap from AsyncTask and set it to

2019-09-09 07:52发布

I am trying to use an AsyncTask to convert an image URL to a bitmap in order to set it to an image. 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. I also have tried to set the bitmap to the image inside 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.

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. This has to be a fairly common task. Why is it so difficult?

How do I reference my images in AsyncTask?

Can I return my bitmap and then set it to the image outside of AsyncTask?

Is there an easier way to set a URL as an image source?

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


}
}

2条回答
戒情不戒烟
2楼-- · 2019-09-09 08:27

Simply define final ImageView variable linked to the view you want to fill with the Bitmap:

     //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:

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*/);
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-09-09 08:30

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. It's made to abstract away all this AsyncTask boilerplate code. 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/

查看更多
登录 后发表回答