使用jsoup解析图像

时间:2015-04-01 04:30:19

标签: java android parsing

AI再次遇到一些问题))

我有一个解析文件链接的代码。进入文件我有一些论坛踏板的链接。像这样:

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

等等。

我尝试使用以下代码解析图像:

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

    }
}

但它仅从最后一个链接解析图像并将其添加到所有项目。我如何制作一些图像bufferedArray或类似的东西?谢谢!!!

P.S。我忘了我的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;
    }
}

1 个答案:

答案 0 :(得分:1)

在这里,您不是要将图像添加到任何列表

            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.
            }

我认为bitmap是某种添加到all elements的字段,所以基本上只存储最后一个状态。您需要做的是在for-loop内添加您的位图。