Skip to content
Related Articles

Related Articles

Improve Article

How to Load SVG from URL in Android ImageView?

  • Difficulty Level : Easy
  • Last Updated : 18 Feb, 2021

It is seen that many Android apps require to use of high-quality images that will not get blur while zooming. So we have to use high-quality images. But if we are using PNG images then they will get blur after zooming because PNG images are made up of pixels and they will reduce their quality after zooming. So SVG images are more preferable to use because SVG images are made up of vectors and they don’t reduce their quality even after zooming. Now we will look at How we can load SVG from its URL in our Android App.

Steps for Loading SVG Image from URL

Step 1: Create a new Android Studio Project

For creating a new Android Studio project just click on File > New > New Project. Make sure to choose your language as JAVA. You can refer to this post on How to Create New Android Studio Project.      

Step 2: Before moving to the coding part add these two dependencies in your build.gradle

Go to Gradle Scripts > build.gradle (Module: app) section and add the following dependencies and click the “Sync Now” on the above pop up. Add these two dependencies.

  •  implementation ‘com.squareup.okhttp3:okhttp:3.10.0’
  •  implementation ‘’

 Step 3: Now we will move towards the design part

Navigate to the app > res > layout > activity_main.xml. Below is the code for the activity_main.xml file.

Note: Drawables are added in app > res > drawable folder. 


<?xml version="1.0" encoding="utf-8"?>
        android:src="@drawable/gfgimage" />

Step 4: Now create a new JAVA class as Utils

In this JAVA class, we are loading data from the URL in the form of the byte stream. The sharp library will convert this byte stream and will load the SVG image in our target ImageView. To create a new JAVA class navigate to the app > java > your apps package name >> Right-click on it and then click on it and click New > Java class. Give a name to your JAVA class. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.content.Context;
import android.widget.ImageView;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Utils {
    private static OkHttpClient httpClient;
    // this method is used to fetch svg and load it into target imageview.
    public static void fetchSvg(Context context, String url, final ImageView target) {
        if (httpClient == null) {
            httpClient = new OkHttpClient.Builder()
                    .cache(new Cache(context.getCacheDir(), 5 * 1024 * 1014))
        // here we are making HTTP call to fetch data from URL.
        Request request = new Request.Builder().url(url).build();
        httpClient.newCall(request).enqueue(new Callback() {
            public void onFailure(Call call, IOException e) {
                // we are adding a default image if we gets any error.
            public void onResponse(Call call, Response response) throws IOException {
                // sharp is a library which will load stream which we generated
                // from url in our target imageview.
                InputStream stream = response.body().byteStream();

Step 5: Now we will use this Utils class in our file to load images from the URL

Navigate to the app > java > your apps package name > file. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    protected void onCreate(Bundle savedInstanceState) {
        // initialize your imageview
        imageView = findViewById(;
        // url for .svg image
        String url = " ";
        // this method will load svg 
          // image in your imageview
        Utils.fetchSvg(this, url, imageView);

Step 6: Add internet permission in your AndroidManifest file

Navigate to the app > manifest and add internet permission.


<?xml version="1.0" encoding="utf-8"?>
    <!--Permission for internet-->
    <uses-permission android:name="android.permission.INTERNET" />
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


SVG from URL in Android Output

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!

My Personal Notes arrow_drop_up
Recommended Articles
Page :