How to download image using Android Volley

In this android programming source code example, we are going to download image using Android Volley.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to download image using Android Volley.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/downloadBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:text="Download"
        android:onClick="downloadBtn"
        android:textColor="#ffffff" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/downloadBtn"
        android:src="@mipmap/ic_launcher" />

</RelativeLayout>

MainActivity.java

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;


public class VolleyActivity18 extends AppCompatActivity implements Response.ErrorListener, Response.Listener<Bitmap>{
    private ImageView imageView;
    private RequestQueue requestQueue;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volley18);
        imageView = (ImageView) findViewById(R.id.imageView);
        requestQueue = Volley.newRequestQueue(this);
    }

    public void downloadBtn(View view){
        progressDialog = new ProgressDialog(VolleyActivity18.this);
        progressDialog.setMessage(getString(R.string.loading));
        progressDialog.setCancelable(false);
        progressDialog.show();
        ImageRequest imageRequest = new ImageRequest("https://www.inducesmile.com/wp-content/uploads/2019/01/inducesmilelog.png",
                this, 600, 600, ImageView.ScaleType.CENTER, null, this);
        requestQueue.add(imageRequest);
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        progressDialog.dismiss();
    }

    @Override
    public void onResponse(Bitmap bitmap) {
        progressDialog.dismiss();
        imageView.setImageBitmap(bitmap);

    }
}

build.gradle

implementation 'com.android.volley:volley:1.1.1'

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment