Thursday, 28 December 2017

Downloading Image From Internet in Android


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
tools:context="com.arham.csdevbin.downloadimagefrominternet.MainActivity">

    <
ImageView
       
android:id="@+id/imageView"
       
android:layout_width="300dp"
       
android:layout_height="400dp"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="30dp"
       
app:layout_constraintLeft_toLeftOf="parent"
       
app:layout_constraintRight_toRightOf="parent"
       
app:srcCompat="@mipmap/ic_launcher"
       
tools:layout_editor_absoluteY="45dp" />

    <
Button
       
android:id="@+id/button"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="20dp"
       
android:text="Download Image"
       
app:layout_constraintLeft_toLeftOf="parent"
       
app:layout_constraintRight_toRightOf="parent"
       
tools:layout_editor_absoluteY="152dp" />
</
LinearLayout>


MainActivity.java
package com.arham.csdevbin.downloadimagefrominternet;

  import android.app.ProgressDialog;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.os.AsyncTask;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.ImageView;
  import java.io.InputStream;
  import java.net.URL;

  public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView imageView;
    Button btnD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = findViewById(R.id.imageView);
        btnD = findViewById(R.id.button);
        btnD.setOnClickListener(MainActivity.this);
    }
    @Override
    public void onClick(View v) {
        DownloadImageTask downloadImageTask = new DownloadImageTask(MainActivity.this);
        downloadImageTask.execute("http://www.gettyimages.ca/gi-resources" +
                "/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg");
    }  

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

        ProgressDialog progressDialog;
        Context context;

        public DownloadImageTask(Context context) {
            this.context = context;
            progressDialog = new ProgressDialog(context);
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog.setMessage("Downloading Image... Please Wait");
            progressDialog.show();
        }
        @Override
        protected Bitmap doInBackground(String... params) {
            String stringUrl = params[0];
            Bitmap bitmap = null;

            try {
                URL url = new URL(stringUrl);
                InputStream inputStream = url.openStream();
                bitmap = BitmapFactory.decodeStream(inputStream);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return bitmap;
        }
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);

            imageView.setImageBitmap(bitmap);
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }
    }
}



No comments:

Post a Comment