Nancy Khorevich
Nancy Khorevich

Reputation: 53

parse images with jsoup

AI have some problems again))

I have a code which parse links from file. Into the file i have some links to forum treads. Like this:

http://vao-priut.org/image/cherepashka-metis-gollandskoi-ovcharki-s-72 http://vao-priut.org/image/taiga-s-26

and etc.

I try to parse images with this code:

class ParseMyPageTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        try {
            input = new URL("https://gist.githubusercontent.com/akhorevich/5b849373dc9abaf921b3/raw/18e79ab6a0c0be007a2a4590e4e176184ced311a/links");

            sc = new Scanner(input.openStream());
            while(sc.hasNextLine()){
                String link = sc.nextLine();

                doc = Jsoup.connect(link).get();
               Elements names = doc.select("div.node-title");
              // Elements images = doc.select("div.node div.content img");
                Elements imgs = doc.select("div.node div.content img");

                for (Element img : imgs) {

                    Element myImage = img;
                    String imgSrc = myImage.attr("src");
                    InputStream inp = new java.net.URL(imgSrc).openStream();
                    // Decode Bitmap
                    bitmap = BitmapFactory.decodeStream(inp);
                }


                for(Element name: names) {
                    mData.add(name.text());

                }

                if(mData.size() == 0) {
                    mData.add("Empty result");
                }

            }



        } catch (IOException e) {
            e.printStackTrace();
            mData.clear();
            mData.add("Exception: " + e.toString());
        }
        return text; // получаем весь текст
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        sectorC_adapter = new SectorC_Adapter(getActivity());
        mListView.setAdapter(sectorC_adapter);

    }
}

But it parse image only from last link and add it to all items. How can i make some images bufferedArray or something like this? Thank you!!!

P.S. I forget about my BaseAdapter:

class SectorC_Adapter extends BaseAdapter{
    private Context c;

    SectorC_Adapter(Context c){
        this.c = c;
    }
    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            inflater = (LayoutInflater)c
                    .getSystemService(c.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.sector_row, parent,false);
        }else {
            row = convertView;
        }
        tvInfo = (TextView)row.findViewById(R.id.dog_name);
        tvInfo.setText(mData.get(position).toString());
        dog_view = (ImageView)row.findViewById(R.id.dog_view);
        dog_view.setImageBitmap(bitmap);
        return row;
    }
}

Upvotes: 0

Views: 892

Answers (1)

Antoniossss
Antoniossss

Reputation: 32535

In here you are not adding your image to any list

            for (Element img : imgs) {

                Element myImage = img;
                String imgSrc = myImage.attr("src");
                InputStream inp = new java.net.URL(imgSrc).openStream();
                // Decode Bitmap
                bitmap = BitmapFactory.decodeStream(inp);

               ///HERE YOU SHOULD ADD YOUR BITMAP TO ALL ELEMENTS
               ///eg. allImages.add(bitmap); or whatever.
            }

I suppose that bitmap is some sort of field that is added to your all elements so basicly only the last state will be stored. What you have to do is to add your bitmap inside for-loop.

Upvotes: 1

Related Questions