[英]Has asynctask been used correctly in the code below?
i am trying to use asynctask to download images from internet.i have this in my custom pager adapter which sets imageview with the image downloaded. 我试图使用asynctask从internet.i下载图像。我在我的自定义寻呼机适配器中设置了此功能,该适配器将imageview设置为下载的图像。
lllllllllllllllllllllllllllllllllllllllllllllll lllllllllllllllllllllllllllllllllllllllllllllllll lllllllllllllllllllllllllllllllllllllllllllllll lllllllllllllllllllllllllllllllllllllllllllllllll
public class pageradapter extends PagerAdapter {
Button load_img;
ImageView imgview;
Bitmap bitmap;
Context mContext;
LayoutInflater mLayoutInflater;
List<String> l = MainActivity.list;
ImageLoader mImageLoader;
public pageradapter(Context context) {
mContext = context;
}
@Override
public int getCount() {
return 4;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageLoader mImageLoader = ImageLoader.getInstance();
mLayoutInflater = ((LayoutInflater) container.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE));
View view = mLayoutInflater.inflate(R.layout.img, container, false);
imgview = (ImageView) view.findViewById(R.id.imageView3);
new LoadImage().execute(l.get(position));
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout) object);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
public class LoadImage extends AsyncTask<String, String, Bitmap> {
ProgressDialog pDialog = new ProgressDialog(mContext);
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.setMessage("Loading Image ....");
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(args[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap image) {
if(image != null){
imgview.setImageBitmap(image);
pDialog.dismiss();
}else{
pDialog.dismiss();
Toast.makeText(mContext, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
}
images do not appear in the view pager. 图像不会出现在视图寻呼机中。
You should change your LoadImage class. 您应该更改您的LoadImage类。
public class LoadImage extends AsyncTask<String, Void, Bitmap> {
ImageView view;
public LoadImage(ImageView view){
this.view = view;
}
ProgressDialog pDialog = new ProgressDialog(mContext);
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.setMessage("Loading Image ....");
pDialog.setCancelable(false);
pDialog.show();
}
protected Bitmap doInBackground(String... args) {
try {
URL URL = new URL(args[0]);
URLConnection connection = URL.openConnection();
connection.connect();
InputStream input = new BufferedInputStream(URL.openStream());
return BitmapFactory.decodeStream(input);
} catch (Exception e) {}
return null;
}
protected void onPostExecute(Bitmap image) {
if (pDialog.isShowing())
pDialog.dismiss();
if(image != null){
view.setImageBitmap(image);
}else{
Toast.makeText(mContext, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
Use it like this: 像这样使用它:
new LoadImage(imgview).execute(l.get(position)); 新的LoadImage(imgview).execute(l.get(position));
I would argue that this particular case is not a best practice for use of an AsyncTask. 我认为这种特殊情况不是使用AsyncTask的最佳实践。 Instead, try using a library called Glide that is excellent and downloading images and placing them in an ImageView. 相反,请尝试使用名为Glide的库,该库非常出色,可以下载图像并将其放置在ImageView中。 You'll write a lot less code and have fewer problems with a better user experience. 您将编写更少的代码,并减少具有更好用户体验的问题。
To set image from url to ImageView use library like http://square.github.io/picasso/ 要将图像从url设置为ImageView,请使用类似http://square.github.io/picasso/的库
Its so simple to use just add dependency in project 它是如此简单易用,只需在项目中添加依赖项
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Here is some libs for loading image from URL and it will store image in to memory as cache. 这是一些用于从URL加载图像的库,它将把图像作为缓存存储在内存中。
UIL : flexible and highly customizable instrument for image loading, caching and displaying. UIL :灵活且高度可定制的工具,用于图像加载,缓存和显示。 It provides a lot of configuration options and good control over the image loading and caching process. 它提供了许多配置选项,并且可以很好地控制图像加载和缓存过程。
PICASSO : Handling ImageView recycling and download cancelation in an adapter. PICASSO :在适配器中处理ImageView回收和下载取消。 Complex image transformations with minimal memory use. 复杂的图像转换,使用最少的内存。 Automatic memory and disk caching. 自动内存和磁盘缓存。
i personally prefer picasso. 我个人更喜欢毕加索。
eg picasso 例如毕加索
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.