0

I have an app that works perfectly online - I have webview that loads gallery. However I want this app to works also offline. I receive post details in html format :

<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>

I need to get links to photos to download images and save the, internally. For example :

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

Any help, please how to archieve this ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Bartos
  • 1,007
  • 3
  • 15
  • 38

1 Answers1

2

You have 3 steps to do this.

  1. You should find images in html page, which is an easy task with Jsoup as @greenapps recommended. If your images are loading with javascript( if you use angular or react etc..) you can get their links with using WebClient.
  2. Secondly, you should download images from network to internal storage. I recommend Volley for this approach which is a officially supported library from 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. You need to save Bitmap response(file) to your internal storage. You can check this link below. Save bitmap to location

Abdullah Tellioglu
  • 1,434
  • 1
  • 10
  • 26