我找不到任何有关将所有图像发送到html网页的代码

时间:2019-04-15 23:52:42

标签: java android jsoup

我尝试了很多方法,但没有成功 而且我知道使用了8a93b54b-c75a-49b5-a5be-9060715b1a33 Microsoft-Windows-DirectSound ae4bd3be-f36f-45b6-8d21-bdd6fb832853 Microsoft-Windows-Audio 方法,我的意思是无论如何都无法创建关于它的循环 此处的网页:http://www.planecrashinfo.com/recent.htm 已经感谢 我的新代码如下 我不知道我如何同步他说的话

First();

我不知道我该怎么写这段代码beetwen scops private class FetchImageLogo extends AsyncTask<Void, Void, Void> { Bitmap bitmap; @Override protected void onPreExecute() { super.onPreExecute(); progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("LOGO"); progressDialog.setMessage("Logo Çekiliyor..."); progressDialog.setIndeterminate(false); progressDialog.show(); } @Override protected Void doInBackground(Void... params) { try{ getAllImages(yemekURL); for (int i = 0; i < imagesURLs.size(); i++) { Glide.with(activity_main) .load(imagesURLs.get(i)) .diskCacheStrategy(DiskCacheStrategy.NONE) .skipMemoryCache(true) .into(resimler); } /* Document doc = Jsoup.connect(yemekURL).get(); Element link = doc.select("img").first(); String absHref = link.attr("abs:src"); // "http://jsoup.org/" InputStream input = new java.net.URL(absHref).openStream(); bitmap = BitmapFactory.decodeStream(input);*/ }catch (Exception e){ e.printStackTrace(); } return null; } ArrayList<String> getAllImages(String url) { try { doc = Jsoup.connect(url).get(); } catch (IOException e) { // ToDo "Can not connect the URL, Make sure that the URL starts with [http://...]" return null; } Elements media = doc.select("[src]"); Element link = doc.select("img").first(); for (Element src : media) { if (src.tagName().equals("img")) { // Add images url to the list imagesURLs.add(link.attr("abs:src")); } } return imagesURLs; } @Override protected void onPostExecute(Void aVoid) { logo_layout = (LinearLayout)findViewById(R.id.logo_layout); ImageView img_logo = (ImageView)findViewById(R.id.img_logo); logo_layout.setVisibility(View.VISIBLE); img_logo.setImageBitmap(bitmap); progressDialog.dismiss(); }

2 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码

ArrayList<String> getAllImages(String url) {
    ArrayList<String> imagesURLs = new ArrayList<>();

    try {
        doc = Jsoup.connect(url).get();
    } catch (IOException e) {
        // ToDo "Can not connect the URL, Make sure that the URL starts with [http://...]"
        return null;
    }

    Elements media = doc.select("[src]");

    for (Element src : media) {
        if (src.tagName().equals("img")) {
            // Add images url to the list
            imagesURLs.add(image.attr("abs:src"));
        }
    }

    return imagesURLs;
}

然后,您可以使用Glide库通过其URL下载,兑现和显示图像:

for (int i = 0; i < imagesURLs.size(); i++) {
    Glide.with(activity)
                .load(imagesURLs.get(i))
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .skipMemoryCache(true)
                .into(imageView);
}

答案 1 :(得分:0)

  1. 您需要将其添加到AndroidManifest.xml的application标签之外才能访问Internet。

    <uses-permission android:name="android.permission.INTERNET" />

  2. GlideJsoup库添加到您的项目中。

  3. activity_main.xml

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="First Image"
        android:gravity="center"
        android:textSize="18sp"/>
    
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
  4. MainActivity.java

    private ProgressDialog progressDialog;
    private ImageView imageView;
    private ArrayList<String> imagesURLs = new ArrayList<>();
    private String firstImgURL = "";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_images);
    
        imageView = findViewById(R.id.imageView);
    
        new FetchImageLogo().execute();
    }
    
    private class FetchImageLogo extends AsyncTask<Void, Void, Void> {
        //Bitmap bitmap;
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setTitle("LOGO");
            progressDialog.setMessage("Logo Çekiliyor...");
            progressDialog.setIndeterminate(false);
            progressDialog.show();
        }
    
        @Override
        protected Void doInBackground(Void... params) {
            String yemekURL = "http://www.planecrashinfo.com/recent.htm";
    
            try {
                imagesURLs = getAllImages(yemekURL);
    
                // Can not get images URLs
                if (imagesURLs == null) return null;
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            // Display the first image...
            // you have to put your code in runOnUiThread() to reach UI components
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Glide.with(MainActivity.this)
                            .load(firstImgURL)
                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                            .skipMemoryCache(true)
                            .into(imageView);
                }
            });
    
    
            final StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < imagesURLs.size(); i++) {
                // ToDo: you need to create [RecyclerView] to display all images
                /*runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.with(MainActivity.this)
                                .load(imagesURLs.get(i))
                                .diskCacheStrategy(DiskCacheStrategy.NONE)
                                .skipMemoryCache(true)
                                .into(imageView);
                    }
                });*/
    
                stringBuilder.append("img").append(String.valueOf(i + 1)).append(": ")
                        .append(imagesURLs.get(i)).append("\n");
            }
    
            // See -> Logcat
            Log.d("Debug_Images", stringBuilder.toString());
    
            return null;
        }
    
        ArrayList<String> getAllImages(String url) {
            Document doc;
    
            try {
                doc = Jsoup.connect(url).get();
            } catch (IOException e) {
                // ToDo "Can not connect the URL, Make sure that the URL starts with [http://...]"
                return null;
            }
    
            // Get the first image URL
            Element link = doc.select("img").first();
            firstImgURL = link.attr("abs:src");
    
            // Get all images URLs
            Elements media = doc.select("[src]");
            for (Element img : media) {
                if (img.tagName().equals("img")) {
                    // Add images url to the list
                    imagesURLs.add(img.attr("abs:src"));
                }
            }
    
            return imagesURLs;
        }
    
        @Override
        protected void onPostExecute(Void aVoid) {
            /*logo_layout = (LinearLayout) findViewById(R.id.logo_layout);
            ImageView img_logo = (ImageView) findViewById(R.id.img_logo);
            logo_layout.setVisibility(View.VISIBLE);
            img_logo.setImageBitmap(bitmap);*/
            progressDialog.dismiss();
        }
    }