OliverDamon
OliverDamon

Reputation: 147

Pass JSoup to String []

I want to get several images of a URL and move to a String [] and then play to a slide.

In the case I have already been able to do this with help is listing normal in the Log. However I am not sure how to transform into a String []. I did this with JSoup.

It would look like this: [http://teste.com/image1.png, http://teste.com/image2.png]

public class ImageScrapAsyncTask extends AsyncTask<String, Void, Document> {

        @Override
        protected Document doInBackground(String... urls) {
            try {
                return Jsoup.connect(urls[0]).get();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(Document document) {
            if (document != null) {
                Elements imgElements = document.select("img");
                List<String> images = new ArrayList<>();

                for (Element element : imgElements) {
                    String image = element.attr("data-src");
                    Log.d("IMAGE_URL", image);
                    if(image!=null && !image.equals("")){
                        images.add(image);
                    }
                }


            }
        }
        }

Upvotes: 0

Views: 35

Answers (1)

Saikrishna Rajaraman
Saikrishna Rajaraman

Reputation: 3273

Try this,

String[] imagesArray = images.toArray(new String[0]);

Upvotes: 1

Related Questions