skip to Main Content

I’m new to Android Studio. I’m trying to design an activity where, given a URL, I can click on some button that allows me to extract the first 20 images that it finds on the webpage.

Is there some way to do this in Java without a library?

2

Answers


  1. If you are using web page url then use webview class in android.here is the website document
    https://developer.android.com/reference/android/webkit/WebView

    otherwise you creating own layout then use 2 famous dependencies:

    1.Glide
    2.Picasso
    
    Login or Signup to reply.
  2. You need Asynctask

    1. create a Bitmap variable
    2. open an InputStream with the url that is passed
    3. use BitmapFactory.decodeStream() on the InputStream
    4. assign it to the created Bitmap variable
    5. Then in the onPostExecute method, you simply use setImageBitmap on the ImageView

    –>

    private class DownloadImageFromUrl extends AsyncTask<String, Void,Bitmap> {
                ImageView bmImage;
                public DownloadImageFromUrl(ImageView bmImage) {
                    this.bmImage = bmImage;
                }
            
                protected Bitmap doInBackground(String... urls) {
                    String urldisplay = urls[0];
                    Bitmap bmp = null;
                    try {
                        InputStream in = new java.net.URL(urldisplay).openStream();
                        bmp = BitmapFactory.decodeStream(in);
                    } catch (Exception e) {
                        Log.e("Error", e.getMessage());
                        e.printStackTrace();
                    }
                    return bmp;
                }
                protected void onPostExecute(Bitmap result) {
                    bmImage.setImageBitmap(result);
                }
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search