使用布局充气机延迟加载图像

发布于 2024-12-22 00:57:48 字数 2609 浏览 1 评论 0原文

我是 android 新手,所以在

我想做的事情上寻求一些帮助:

我正在下载一个 xml 文件,将其加载到一个对象中,并使用布局充气器显示它。

在 xml 中有一个图像链接,然后我尝试启动一个单独的异步任务来延迟加载给定链接中的图像。 xml下载工作正常,它只是我正在努力解决的图像位。我想我已经快到了。任何帮助将非常欢迎

我的代码:

 class MyRowAdapter extends ArrayAdapter<XMLItem> {
     Activity context;

     MyRowAdapter (Activity context) {
         super(context, R.layout.news_row, items);
         this.context=context;
     }
 public View getView(int position, View convertView, ViewGroup parent) {

     View row = convertView;
     if(row==null){
         LayoutInflater inflater=context.getLayoutInflater();
         row = inflater.inflate(R.layout.news_row, null);
     }
     XMLItem _item = items.get(position);
     rowpos = position;
     Log.v(TAG,""+position);
     TextView headline = (TextView)row.findViewById(R.id.Headline);
     headline.setText(_item.getHeadline());
     TextView imagepath = (TextView)row.findViewById(R.id.Imagepath);
     imagepath.setText(_item.getImagepath());




     try {
        ImageView img = (ImageView) row.findViewById(R.id.storyimage);
        img.setImageBitmap(_item.getBitmap());
     } catch(IndexOutOfBoundsException e) {
        new DownloadImageTask().execute(_item.getImagepath());

     }




     return row;
 }


         }



 private class DownloadImageTask extends AsyncTask<String,Integer,Bitmap>{
        private ImageView img;

        @Override
        protected Bitmap doInBackground(String... urls) {

            Bitmap bmImg = null;


            try {
                URL imageurl = new URL(urls[0]);
                Log.v("imageurl","="+imageurl);
                HttpURLConnection conn= (HttpURLConnection)imageurl.openConnection();
                   conn.setDoInput(true);
                   conn.connect();
                   InputStream is = conn.getInputStream();

                   bmImg = BitmapFactory.decodeStream(is);




              } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
            //HttpURLConnection conn =(HttpURLConnection)imageurl.openConnection();



        }

            return bmImg;
    }

        @Override
        protected void onPostExecute(Bitmap result) {
            // TODO Auto-generated method stub
            XMLItem _item = items.get(rowpos);
            super.onPostExecute(result);
            img.setImageBitmap(result);
            _item.setBitmap(result);
            Log.v("image task","onpostexecute");
            myRowAdapter.notifyDataSetChanged();
                }

            }

i am new to android, so seeking some help on this

what i am trying to do:

i am downloading an xml file, loading it into an object, and displaying it using a layout inflator.

within the xml there is a link to an image, i am then trying to launch a seperate asynctask to lazy load the images from the link given. the xml download works fine, its just the images bit i am struggling on. i think i am nearly there. any help would be greatly welcome

my code:

 class MyRowAdapter extends ArrayAdapter<XMLItem> {
     Activity context;

     MyRowAdapter (Activity context) {
         super(context, R.layout.news_row, items);
         this.context=context;
     }
 public View getView(int position, View convertView, ViewGroup parent) {

     View row = convertView;
     if(row==null){
         LayoutInflater inflater=context.getLayoutInflater();
         row = inflater.inflate(R.layout.news_row, null);
     }
     XMLItem _item = items.get(position);
     rowpos = position;
     Log.v(TAG,""+position);
     TextView headline = (TextView)row.findViewById(R.id.Headline);
     headline.setText(_item.getHeadline());
     TextView imagepath = (TextView)row.findViewById(R.id.Imagepath);
     imagepath.setText(_item.getImagepath());




     try {
        ImageView img = (ImageView) row.findViewById(R.id.storyimage);
        img.setImageBitmap(_item.getBitmap());
     } catch(IndexOutOfBoundsException e) {
        new DownloadImageTask().execute(_item.getImagepath());

     }




     return row;
 }


         }



 private class DownloadImageTask extends AsyncTask<String,Integer,Bitmap>{
        private ImageView img;

        @Override
        protected Bitmap doInBackground(String... urls) {

            Bitmap bmImg = null;


            try {
                URL imageurl = new URL(urls[0]);
                Log.v("imageurl","="+imageurl);
                HttpURLConnection conn= (HttpURLConnection)imageurl.openConnection();
                   conn.setDoInput(true);
                   conn.connect();
                   InputStream is = conn.getInputStream();

                   bmImg = BitmapFactory.decodeStream(is);




              } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
            //HttpURLConnection conn =(HttpURLConnection)imageurl.openConnection();



        }

            return bmImg;
    }

        @Override
        protected void onPostExecute(Bitmap result) {
            // TODO Auto-generated method stub
            XMLItem _item = items.get(rowpos);
            super.onPostExecute(result);
            img.setImageBitmap(result);
            _item.setBitmap(result);
            Log.v("image task","onpostexecute");
            myRowAdapter.notifyDataSetChanged();
                }

            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微凉 2024-12-29 00:57:48

Instead of implementing the separate task for downloading image from the specified URL, i would suggest you to implement: Android - How do I do a lazy load of images in ListView

故人爱我别走 2024-12-29 00:57:48

我没有看到 DownloadImageTask 中初始化 img 变量的地方。可能您忘记将其传递给构造函数:

private class DownloadImageTask extends AsyncTask<String,Integer,Bitmap> {
    private ImageView img;

    public DownloadImageTask(ImageView img) {
        this.img = img;
    }
...
}

但通常我建议使用 UniversalImageLoader 用于延迟图像加载和显示。如果需要,它可以将您的图像缓存在内存和/或文件系统(SD 卡)中。

简单的使用示例:

ImageLoader.getInstance().displayImage(imageUrl, imageView);

它将真正简化您的生活。

I don't see the place where img variable is initialized in DownloadImageTask. May be you forgot pass it to constructor:

private class DownloadImageTask extends AsyncTask<String,Integer,Bitmap> {
    private ImageView img;

    public DownloadImageTask(ImageView img) {
        this.img = img;
    }
...
}

But generally I recommend use UniversalImageLoader for lazy image loading and displaying. It can cache your images in memory and/or on file system (SD card) if you need.

Simple usage example:

ImageLoader.getInstance().displayImage(imageUrl, imageView);

It will really simplify your life.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文