Привет! Мне нужна ваша помощь.
Пишу приложение на Android, где по при помощи jsoup парсится ссылка на текстовый файл, в которым ссылки на страницы форума. Файл и ссылки в нем мною периодически обновляются извне.
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;
}
}
Примерно так это все выглядит. Но есть две проблемы -
Картинки парсятся только из первой ссылки в файле (у меня конкретно собаки - допустим во все элементы вставляются разные фотки Жучки, а нужно чтоб каждой ссылке соответствовала своя фотка собаки)
Вторая проблема - слишком это жрет память и приложение периодически "падает". Можно ли как то фотографии грузить в буфер или на карту или куда еще, а потом подгружать?
Спасибо большое!