0

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

            }
Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70

2 Answers2

0

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.

nostra13
  • 12,377
  • 3
  • 33
  • 43
0

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

Community
  • 1
  • 1
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295