我如何实现异步任务这一形象有何看法? [英] How do I implement this image view in an async task?
问题描述
我必须传递给一个活动的网址,我试图表现出从网址全屏幕上的图像,但它抛出一个主网线异常。
这是我能找到我认为我必须把方法异步任务,但是我似乎无法理解它在所有。所以,我将如何把这个方法在异步任务?
FullScreenImageView.java
公共类FullscreenImageView延伸活动{
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 字符串URL = getIntent()getStringExtra(SelectedImageURL); 尝试{
ImageView的I =(ImageView的)findViewById(R.id.imgView);
位图位图= BitmapFactory.de codeStream((InputStream的)新的URL(网址).getContent());
i.setImageBitmap(位图);
}赶上(MalformedURLException的E){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}}
}
应该是这样的。
在 doInBackground
你得到的图像,并在 onPostExecute
设置为
私有类DownloadFilesTask扩展的AsyncTask<弦乐,太虚,位图> {
@覆盖
保护位图doInBackground(字符串的URL ...){
位图位图= NULL;
尝试{
位= BitmapFactory.de codeStream((InputStream的)新的URL(网址[0])的getContent());
}赶上(MalformedURLException的E){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
}
返回位图;
}
@覆盖
保护无效onPostExecute(位图位图){
ImageView的I =(ImageView的)findViewById(R.id.imgView);
i.setImageBitmap(位图);
}
}
然后,你把它叫做你的的onCreate
方法内
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
字符串URL = getIntent()getStringExtra(SelectedImageURL);
新DownloadFilesTask().execute(URL);
}
I have an url passed to an activity and I am trying to show the image from the url full screen, however it throws a main network thread exception.
From what I can find I believe I have to put the method in an async task however I cannot seem to make sense of it at all. So how would I put this method in an async task?
FullScreenImageView.java
public class FullscreenImageView extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = getIntent().getStringExtra("SelectedImageURL");
try {
ImageView i = (ImageView)findViewById(R.id.imgView);
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(url).getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
It should be something like this.
In the doInBackground
you get the image, and in the onPostExecute
you set it
private class DownloadFilesTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream((InputStream)new URL(urls[0]).getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
ImageView i = (ImageView)findViewById(R.id.imgView);
i.setImageBitmap(bitmap);
}
}
Then, you call it inside your onCreate
method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = getIntent().getStringExtra("SelectedImageURL");
new DownloadFilesTask ().execute(url);
}
这篇关于我如何实现异步任务这一形象有何看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!