我正在尝试使用asynctask从internet下载图像。我在我的自定义寻呼机适配器中设置了图像视图,下载了图像。
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();
}
}
}
}
图像不会出现在视图寻呼机中。
答案 0 :(得分:1)
您应该更改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();
}
}
}
像这样使用:
new LoadImage(imgview).execute(l.get(position));
答案 1 :(得分:0)
我认为这种特殊情况不是使用AsyncTask的最佳实践。相反,请尝试使用名为Glide的库,该库非常出色并下载图像并将其放置在ImageView中。您可以编写更少的代码,减少用户体验方面的问题。
答案 2 :(得分:0)
要将图像从url设置为ImageView,请使用https://support.google.com/mail/answer/78754
等库使用起来非常简单,只需在项目中添加依赖项
即可Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
答案 3 :(得分:0)