All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Android Imageview Set Image From URL

Last Updated : Mar 11, 2024

Android Imageview Set Image From URL

In this article we will show you the solution of android imageview set image from url, several programmes use 3ed-party APIs like Glide or Picasso to load image from the internet for display.

This indicates that for these programmes to continue functioning properly, they partially rely on these services.

Instead of relying on these services, one should build their own code to improve the programme.

In this article, we'll show you how easy it is to add photographs to our application without using a third-party server.

You discovered how to obtain information from a web service, parse the answer, and create a Kotlin object in the previous codelab.

With that information, you may load and show photographs from a website URL in this codelab.

When adding images in your Android application, all you need to do is put the image inside the drawable folder and change the ImageView source ("src") field of your layout xml file to an appropriate path.

Step By Step Guide On Android Imageview Set Image From URL :-

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;
    public DownloadImageTask(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);
    }
}
  1. First we have to create public classes in compact, medium, and expanded formats.
  2. Then we create the class as the main activity.
  3. After that Replace with a known container that you can safely add a view to where it won't affect the layout and the view and won't be replaced.
  4. Then we Add a utility view to the container to hook.
  5. Then we added View. The configuration changed. This is required for all activities, even those that don't handle configuration changes.
  6. We also can't use Activity. On configuration changed, since there are situations where that won't be called when the configuration changes.

Conclusion :-

RecyclerView with the possible exception of the ImageView, the adapter class is configured properly and updates your views.

It's simplest to create a loader inner class and extend AsyncTask in order to acquire the picture from the URL,which you can now only access as just a string variable thru the adapter.

To avoid locking up your app just on user while this downloads each of the images, you should complete this process asynchronously.

I hope this article on android imageview set image from url helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪