Download image with showing horizontal progress bar dialog android programmatically

How to download image file from server using HTTP URL and save-store in SD card storage on button click with progress dialog coding example.

Image download task can be easily done in android application using AsyncTask class. But in this tutorial we are downloading the image from HTTP URL with showing progress dialog inside it with percentage text, Means it will download the whole image on button click and at the download time it will show horizontal progress dialog with percentage bar fill method. So the bar will fill depending on your mobile internet connection download speed. After done downloading it will store the image inside Memory card( If available ) else save into mobile phone memory and set that downloaded image inside imageview. So here is the complete step by step tutorial for Download image with showing horizontal progress bar dialog android programmatically.

android-project-download-code-button

How to Download image with showing horizontal progress bar dialog android programmatically.

Please add INTERNET permission and WRITE_EXTERNAL_STORAGE permission to your AndroidManifest.xml file.

 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Code for MainActivity.java file.

package com.downloadimageshowinghorizontalprogressbar_android_examples.com;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends Activity {

 Button button;
 ProgressDialog progressdialog;
 public static final int Progress_Dialog_Progress = 0;
 String ImageURL = "https://www.android-examples.com/wp-content/uploads/2016/04/demo_download_image.jpg" ;
 URL url;
 URLConnection urlconnection ;
 int FileSize;
 InputStream inputstream;
 OutputStream outputstream;
 byte dataArray[] = new byte[1024];
 long totalSize = 0;
 ImageView imageview;
 String GetPath ;
 
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 button = (Button)findViewById(R.id.button1);
 imageview = (ImageView)findViewById(R.id.imageView1);
 
 button.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 
 new ImageDownloadWithProgressDialog().execute(ImageURL);
 
 }
 });
 }

public class ImageDownloadWithProgressDialog extends AsyncTask<String, String, String> {
 
 @Override
 protected void onPreExecute() {
 
 super.onPreExecute();
 
 showDialog(Progress_Dialog_Progress);
 }

 @Override
 protected String doInBackground(String... aurl) {
 
 int count;

 try {

 url = new URL(aurl[0]);
 urlconnection = url.openConnection();
 urlconnection.connect();

 FileSize = urlconnection.getContentLength();

 inputstream = new BufferedInputStream(url.openStream());
 outputstream = new FileOutputStream("/sdcard/demo_photo1.jpg");


 while ((count = inputstream.read(dataArray)) != -1) {
 
 totalSize += count;
 
 publishProgress(""+(int)((totalSize*100)/FileSize));
 
 outputstream.write(dataArray, 0, count);
 }

 outputstream.flush();
 outputstream.close();
 inputstream.close();
 
 } catch (Exception e) {}
 return null;

 }
 protected void onProgressUpdate(String... progress) {
 
 progressdialog.setProgress(Integer.parseInt(progress[0]));
 }

 @Override
 protected void onPostExecute(String unused) {
 
 dismissDialog(Progress_Dialog_Progress);
 
 GetPath = Environment.getExternalStorageDirectory().toString() + "/demo_photo1.jpg";
 
 imageview.setImageDrawable(Drawable.createFromPath(GetPath));
 
 Toast.makeText(MainActivity.this, "Image Downloaded Successfully", Toast.LENGTH_LONG).show();
 }
}

@Override
protected Dialog onCreateDialog(int id) {
 switch (id) {
 case Progress_Dialog_Progress:
 
 progressdialog = new ProgressDialog(MainActivity.this);
 progressdialog.setMessage("Downloading Image From Server...");
 progressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
 progressdialog.setCancelable(false);
 progressdialog.show();
 return progressdialog;
 
 default:
 
 return null;
 }
}

}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.downloadimageshowinghorizontalprogressbar_android_examples.com.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_marginTop="250dp"
 android:text="Click here to Download image by showing horizontal progress bar dialog android programmatically" />

 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 />

</RelativeLayout>

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.downloadimageshowinghorizontalprogressbar_android_examples.com"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />

 <uses-permission android:name="android.permission.INTERNET"/>
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 
 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>

Screenshots:

Click here to download Download image with showing horizontal progress bar dialog android programmatically project with source code.

Leave a Reply

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