从HTML库中获取照片链接

时间:2018-03-16 10:07:02

标签: android html

我有一个完全在线工作的应用程序 - 我有webview加载库。但是,我希望这个应用程序也能脱机工作。我收到html格式的帖子详细信息:

<p><img class="alignnone size-medium wp-image-8" src="http://x.pl/wp-content/uploads/2018/02/Jellyfish-300x225.jpg" alt="" width="300" height="225" srcset="http://x.pl/wp-content/uploads/2018/02/Jellyfish-300x225.jpg 300w, http://x.pl/wp-content/uploads/2018/02/Jellyfish-768x576.jpg 768w, http://x.pl/wp-content/uploads/2018/02/Jellyfish.jpg 1024w" sizes="(max-width: 300px) 100vw, 300px" /> <img class="alignnone size-medium wp-image-7" src="x.pl/wp-content/uploads/2018/02/Hydrangeas-300x225.jpg" alt="" width="300" height="225" srcset="x.pl/wp-content/uploads/2018/02/Hydrangeas-300x225.jpg 300w, x.pl/wp-content/uploads/2018/02/Hydrangeas-768x576.jpg 768w, http://x.pl/wp-content/uploads/2018/02/Hydrangeas.jpg 1024w" sizes="(max-width: 300px) 100vw, 300px" /> <img class="alignnone size-medium wp-image-6" src="http://x.pl/wp-content/uploads/2018/02/Desert-300x225.jpg" alt="" width="300" height="225" srcset="http://x.pl/wp-content/uploads/2018/02/Desert-300x225.jpg 300w, http://x.pl/wp-content/uploads/2018/02/Desert-768x576.jpg 768w, http://x.pl/wp-content/uploads/2018/02/Desert.jpg 1024w" sizes="(max-width: 300px) 100vw, 300px" /> <img class="alignnone size-medium wp-image-5" src="http://x.pl/wp-content/uploads/2018/02/Chrysanthemum-300x225.jpg" alt="" width="300" height="225" srcset="http://x.pl/wp-content/uploads/2018/02/Chrysanthemum-300x225.jpg 300w, http://x.pl/wp-content/uploads/2018/02/Chrysanthemum-768x576.jpg 768w, http://x.pl/wp-content/uploads/2018/02/Chrysanthemum.jpg 1024w" sizes="(max-width: 300px) 100vw, 300px" /></p>

我需要获取照片链接以下载图像并在内部保存。 例如:

String link = http://x/wp-content/uploads/2018/02/Jellyfish-300x225.jpg

任何帮助,请如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

您有3个步骤来执行此操作。

  1. 您应该在html页面中找到图像,这是一个简单的任务,使用Jsoup作为@greenapps推荐。如果您的图像使用javascript加载(如果您使用角度或反应等),您可以使用WebClient获取其链接。
  2. 其次,您应该将图像从网络下载到内部存储。我建议Volley采用这种方法,这是Google正式支持的库。

            // Initialize a new ImageRequest
            ImageRequest imageRequest = new ImageRequest(
                    mImageURLString, // Image URL In this case your http://x/wp-content/uploads/2018/02/Jellyfish-300x225.jpg
                    new Response.Listener<Bitmap>() { // Bitmap listener
                        @Override
                        public void onResponse(Bitmap response) {
                            // Do something with response
                            mImageView.setImageBitmap(response);
                            // Save this downloaded bitmap to internal storage
                            Uri uri = saveImageToInternalStorage(response);
    
                            // Display the internal storage saved image to image view
                            mImageViewInternal.setImageURI(uri);
                        }
                    },
                    0, // Image width
                    0, // Image height
                    ImageView.ScaleType.CENTER_CROP, // Image scale type
                    Bitmap.Config.RGB_565, //Image decode configuration
                    new Response.ErrorListener() { // Error listener
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            // Do something with error response
                            error.printStackTrace();
    
                        }
                    }
            );
    
  3. 您需要将位图响应(文件)保存到内部存储。您可以在下面查看此链接。 Save bitmap to location