如何从AsyncTask的得到一个位图并将其设置为我的形象 [英] How do I get a bitmap from AsyncTask and set it to my image
问题描述
我想使用的AsyncTask将图像URL转换为位图,以便将其设置为一个图像。我不明白如何让位图出的AsyncTask的,这样我可以将其设置为我的我创建在一个循环中的图像。我还试图位图设置为内部的AsyncTask图像。我的理解是,一切都要里面的AsyncTask做,但我不能引用图片我想设置的值。
我研究的URL转换位图和AsyncTask的,但我无法弄清楚如何将它们结合起来,得到我想要的东西。这已经是一个相当普遍的任务。为什么这么难呢?
我如何引用我的图片AsyncTask的?
我能回到我的位图,然后将其设置为图像外的AsyncTask的?
有没有设定一个URL作为图像源的更简单的方法?
我已经包括了我有这么远。</ P>
感谢您的帮助,并抽出时间来阅读。
类BitmapTaks扩展的AsyncTask&LT;弦乐,太虚,位图&GT; {公共位图doInBackground(字符串的URL ...){
位图BM = NULL;
尝试{
网址aURL =新的URL(网址[0]);
康涅狄格州的URLConnection = aURL.openConnection();
conn.connect();
InputStream为= conn.getInputStream();
二BufferedInputStream为=新的BufferedInputStream(是);
BM = BitmapFactory.de codeStream(之二);
bis.close();
is.close();
}赶上(IOException异常五){
Log.e(IMG,错误获取位图,E);
}
返回BM;
}保护无效onPostExecute(位图BM){
//做一些事情后执行
}
}
这已经是一个相当普遍的任务。为什么这么难呢?
块引用>我真的建议你看看到的Android排球库 。它抽象掉这一切的AsyncTask样板code制成。下面是如何设置的图像位图一个非常好的例子。
ImageLoader的ImageLoader的= AppController.getInstance()getImageLoader()。//如果您正在使用正常的ImageView
imageLoader.get(Const.URL_IMAGE,新ImageListener(){@覆盖
公共无效onErrorResponse(VolleyError错误){
Log.e(TAG,图像加载错误:+ error.getMessage());
}@覆盖
公共无效onResponse(ImageContainer响应,布尔ARG1){
如果(response.getBitmap()!= NULL){
//加载图像插入的ImageView
imageView.setImageBitmap(response.getBitmap());
}
}
});您可以阅读从这里更多: HTTP: //www.androidhive.info/2014/05/android-working-with-volley-library-1/
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 } }
解决方案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/
这篇关于如何从AsyncTask的得到一个位图并将其设置为我的形象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!