是否在下面的代码中正确使用了asynctask?

时间:2016-02-09 05:39:19

标签: java android android-asynctask bitmapfactory android-pageradapter

  

我正在尝试使用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();

            }
        }
    }

}
  

图像不会出现在视图寻呼机中。

4 个答案:

答案 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)

以下是一些用于从URL加载图像的库,它会将图像作为缓存存储在内存中。

UIL:灵活且高度可定制的图像加载,缓存和显示工具。它提供了许多配置选项,并且可以很好地控制图像加载和缓存过程。

PICASSO:     处理ImageView回收并在适配器中下载取消。     复杂的图像转换,内存使用最少。自动内存和磁盘缓存。

我个人更喜欢毕加索。

e.g。毕加索

Picasso.with(context)
  .load(url)
  .resize(50, 50)
  .centerCrop()
  .into(imageView)