Hello! I need your help.

I am writing an application on Android, where a link to a text file with links to forum pages is parsed using jsoup. The file and links in it are periodically updated by me from the outside.

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); allImages.add(bitmap); } 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); } } 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(allImages.get(position)); return row; } } 

Something like this all looks like. But there are two problems -

  1. Pictures are parsed only from the first link in the file (I specifically have dogs - let's say different pictures of Bugs are inserted into all the elements, but it’s necessary that each link has its own photo of the dog)

  2. The second problem is that it eats too much memory and the application periodically crashes. Is it possible to load photos into the buffer or onto a map or somewhere else, and then upload?

Thank you very much!

    1 answer 1

    As for the second problem, yes, you can. When you parse the page, you do not need to immediately load the photo into RAM, if you need to save it on the device, then download to the internal memory or sd card, and then take it from there to show. Or you can simply store a link to the image, and load it directly at the time of display. For the second method, there are a bunch of libraries:
    Newfangled from facebook
    Comfortable and powerful
    old and proven picasso

    • thank. The first question is more important because you need to download pictures, and not to litter the application. - akhorevich
    • @akhorevich describe the first problem in more detail - andreich
    • screen It looks like this. On the pictures - the same dog from the first link, the application parses 10 of her pictures. It is necessary that from each link parsilo 1st picture and inserted into its item - akhorevich
    • @akhorevich so you have broken logic. I advise you to begin to redo your decision as I described in the answer. In the new form it will be easier to understand the logic. create a more convenient data model for storing information about a single element and push all the necessary information there - andreich
    • Thank you, @andreich, I decided everything.) - akhorevich