Image download from server Android code

Image download from server Android code will enable download bitmap image for given url. Below is Android code which will download a bitmap image from Url and set it to image view. Here we have defined a constructor to receive the call back from AsyncTask.

public class DownloadImagesTask extends AsyncTask<URL, Void, Bitmap> {

    String TAG = "DownloadImagesTask";
    private final DownloadTaskListner listener;
    private final ImageView iv;

    public DownloadImagesTask(DownloadTaskListner listner, ImageView iv) {
        this.listener = listner;
        this.iv = iv;

    protected Bitmap doInBackground(URL... urls) {
        Log.i(TAG, String.valueOf(urls[0]));
        URL url = urls[0];
        HttpURLConnection connection = null;
            connection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = connection.getInputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
            return bmp;
        }catch(IOException e){
        return null;

    protected void onPostExecute(Bitmap bitmap) {
        if(bitmap!=null) {
                listener.onDownloadComplete(bitmap, iv);

public interface DownloadTaskListner {
    void onDownloadComplete(Bitmap bitmap, ImageView iv);
    void onDownloadError(String error);
public class OrderSummaryActivity extends AppCompatActivity implements DownloadTaskListner{

    ImageView image;

    protected void onCreate(Bundle savedInstanceState) {
        image = findViewByID(
        DownloadImagesTask task = new DownloadImagesTask(this, image);


    public void onDownloadComplete(Bitmap bitmap, ImageView iv) {

    public void onDownloadError(String error) {


    public static URL stringToURL(String urlString){
            URL url = new URL(urlString);
            return url;
        }catch(MalformedURLException e){
        return null;


Image download from server Android code.

Also check :

(Visited 97 times, 1 visits today)


Eyeing investment. Please note that the views given in this website are meant for reference and guidance of the readers to explore further on the topics and take informed decisions. These should not be construed as investment advice or legal opinion.

You may also like...

1 Response

  1. January 11, 2019

    […] jQuery( function($){ if ( $('#flexslider-featured').length ) { $('#flexslider-featured').on('featured-slider-ready', function() { $( '#5c37f00e089fa' ).animateSvg(); }); } else { $( '#5c37f00e089fa' ).animateSvg( { svg_opacity : 0.3, filter_opacity : 0.5 } ); } }); 0 […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
25 − 23 =