【问题标题】:Has asynctask been used correctly in the code below?下面的代码中是否正确使用了 asynctask?
【发布时间】:2016-02-09 05:39:18
【问题描述】:

我正在尝试使用 asynctask 从 Internet 下载图像。我在我的自定义寻呼机适配器中有这个,它使用下载的图像设置 imageview。

llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll 呸呸呸呸呸呸呸呸呸呸呸呸呸

 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();

            }
        }
    }

}

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

【问题讨论】:

  • 为什么不使用NetworkImageView

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


【解决方案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));

【讨论】:

    【解决方案2】:

    我认为这种特殊情况不是使用 AsyncTask 的最佳实践。相反,尝试使用一个名为 Glide 的库,它非常好,可以下载图像并将它们放置在 ImageView 中。您将编写更少的代码和更少的问题,从而获得更好的用户体验。

    【讨论】:

      【解决方案3】:

      要将图像从 url 设置到 ImageView 使用库,如 http://square.github.io/picasso/

      使用非常简单,只需在项目中添加依赖即可

      Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 
      

      【讨论】:

        【解决方案4】:

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

        UIL:用于图像加载、缓存和显示的灵活且高度可定制的工具。它提供了很多配置选项以及对图像加载和缓存过程的良好控制。

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

        我个人更喜欢毕加索。

        例如毕加索

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-30
          • 2014-04-01
          • 1970-01-01
          • 2014-10-25
          • 1970-01-01
          相关资源
          最近更新 更多