Hello,Every One I have Now Write here simple Demo for Loading Image From Url with Help of
AsyncTask in Android,By this Way We Can see that how much data of image has been loading and so we can show progress of Loading Image To ProgressDialog in Android.
Below is Main Activity luncher Class Which is :
LoadBitmapActivity.java
Here is Java Code for It.
___________________________________Start____________________________________________
public class LoadBitmapActivity extends Activity {
/** Called when the activity is first created. */
public boolean isConnected = false;
public static final String IMAGE_URL = "http://media1.santabanta.com/full4/bollywood%20movies/agneepath/agneepath-8a.jpg";
ProgressDialog mProgressDialog;
TextView mTextView;
ImageView mImageView;
Button mButton;
private ByteArrayOutputStream byte_output_stream;
private double filesize;
private double downloaded;
public static final int DOWNLOADING = 0;
private int status = DOWNLOADING;
public static final int COMPLETE = 1;
private Bitmap mBitmap;
HttpURLConnection connection;
InputStream is;
public int MAX_BUFFER_SIZE = 1024;
public static int SIZE = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.text_data);
mImageView = (ImageView) findViewById(R.id.loaded_image);
mButton = (Button) findViewById(R.id.btn_start);
mButton.setOnClickListener(mButtonClickListener);
}
View.OnClickListener mButtonClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start:
if (checkInternet()) {
new DownloadImagetask().execute(IMAGE_URL);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(
LoadBitmapActivity.this);
builder.setTitle(R.string.alert_internet_msg_title);
builder.setPositiveButton(R.string.alert_btn_text,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
}
break;
default:
break;
}
}
};
public boolean checkInternet() {
ConnectivityManager mConnectivityManager = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkinfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkinfo != null && mNetworkinfo.isAvailable()
&& mNetworkinfo.isConnected()) {
return isConnected = true;
} else {
return isConnected = false;
}
}
private class DownloadImagetask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected void onPostExecute(Bitmap result) {
mProgressDialog.dismiss();
mImageView.setImageBitmap(result);
super.onPostExecute(result);
}
public DownloadImagetask() {
mBitmap = null;
downloaded = 0;
filesize = 0;
status = DOWNLOADING;
}
@Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(LoadBitmapActivity.this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// mProgressDialog.setProgress(0);
mProgressDialog.setMessage(getString(R.string.progress_msg_title));
// mProgressDialog.setMax(10);
mProgressDialog.show();
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values) {
mProgressDialog.setProgress(values[0]);
super.onProgressUpdate(values);
}
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
filesize = connection.getContentLength();
byte_output_stream = new ByteArrayOutputStream((int) filesize);
connection.setDoOutput(true);
connection.connect();
is = connection.getInputStream();
if (filesize < SIZE) {
MAX_BUFFER_SIZE = 1024;
} else {
MAX_BUFFER_SIZE = (int) Math.ceil(filesize / SIZE);
}
// loop with step 1Kb
while (status == DOWNLOADING) {
byte[] imagedata_buffer;
if (filesize - downloaded > MAX_BUFFER_SIZE) {
imagedata_buffer = new byte[MAX_BUFFER_SIZE];
} else {
imagedata_buffer = new byte[(int) (filesize - downloaded)];
}
int read = is.read(imagedata_buffer);
if (read == -1) {
publishProgress(100);
break;
}
byte_output_stream.write(imagedata_buffer, 0, read);
downloaded += read;
publishProgress((int) ((downloaded / filesize) * 100));
}
if (status == DOWNLOADING) {
status = COMPLETE;
}
return BitmapFactory
.decodeStream((InputStream) new ByteArrayInputStream(
byte_output_stream.toByteArray()));
} catch (MalformedURLException excep) {
excep.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return null;
}
}
}
-----------------------------------------------End of Class------------------------------------------------
Here is Xml that i have used for Activity.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical" >
<TextView
android:id="@+id/text_data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:id="@+id/loaded_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dip"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:gravity="center"
android:text="@string/btn_start" />
</FrameLayout>
For Work with image to load from Internet you need to give two permission in your's android project Manifest .And Here are two permission.
This Permission Give us to access internet from our android application.
<uses-permission android:name="android.permission.INTERNET"/>
And Now Second one i have used for To get State of our Android Mobile it is connected to internet or not,and this permission also need to get any changes in network state .
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
AsyncTask in Android,By this Way We Can see that how much data of image has been loading and so we can show progress of Loading Image To ProgressDialog in Android.
Below is Main Activity luncher Class Which is :
LoadBitmapActivity.java
Here is Java Code for It.
___________________________________Start____________________________________________
public class LoadBitmapActivity extends Activity {
/** Called when the activity is first created. */
public boolean isConnected = false;
public static final String IMAGE_URL = "http://media1.santabanta.com/full4/bollywood%20movies/agneepath/agneepath-8a.jpg";
ProgressDialog mProgressDialog;
TextView mTextView;
ImageView mImageView;
Button mButton;
private ByteArrayOutputStream byte_output_stream;
private double filesize;
private double downloaded;
public static final int DOWNLOADING = 0;
private int status = DOWNLOADING;
public static final int COMPLETE = 1;
private Bitmap mBitmap;
HttpURLConnection connection;
InputStream is;
public int MAX_BUFFER_SIZE = 1024;
public static int SIZE = 100;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.text_data);
mImageView = (ImageView) findViewById(R.id.loaded_image);
mButton = (Button) findViewById(R.id.btn_start);
mButton.setOnClickListener(mButtonClickListener);
}
View.OnClickListener mButtonClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_start:
if (checkInternet()) {
new DownloadImagetask().execute(IMAGE_URL);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(
LoadBitmapActivity.this);
builder.setTitle(R.string.alert_internet_msg_title);
builder.setPositiveButton(R.string.alert_btn_text,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
}
break;
default:
break;
}
}
};
public boolean checkInternet() {
ConnectivityManager mConnectivityManager = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkinfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkinfo != null && mNetworkinfo.isAvailable()
&& mNetworkinfo.isConnected()) {
return isConnected = true;
} else {
return isConnected = false;
}
}
private class DownloadImagetask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected void onPostExecute(Bitmap result) {
mProgressDialog.dismiss();
mImageView.setImageBitmap(result);
super.onPostExecute(result);
}
public DownloadImagetask() {
mBitmap = null;
downloaded = 0;
filesize = 0;
status = DOWNLOADING;
}
@Override
protected void onPreExecute() {
mProgressDialog = new ProgressDialog(LoadBitmapActivity.this);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// mProgressDialog.setProgress(0);
mProgressDialog.setMessage(getString(R.string.progress_msg_title));
// mProgressDialog.setMax(10);
mProgressDialog.show();
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... values) {
mProgressDialog.setProgress(values[0]);
super.onProgressUpdate(values);
}
@Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
filesize = connection.getContentLength();
byte_output_stream = new ByteArrayOutputStream((int) filesize);
connection.setDoOutput(true);
connection.connect();
is = connection.getInputStream();
if (filesize < SIZE) {
MAX_BUFFER_SIZE = 1024;
} else {
MAX_BUFFER_SIZE = (int) Math.ceil(filesize / SIZE);
}
// loop with step 1Kb
while (status == DOWNLOADING) {
byte[] imagedata_buffer;
if (filesize - downloaded > MAX_BUFFER_SIZE) {
imagedata_buffer = new byte[MAX_BUFFER_SIZE];
} else {
imagedata_buffer = new byte[(int) (filesize - downloaded)];
}
int read = is.read(imagedata_buffer);
if (read == -1) {
publishProgress(100);
break;
}
byte_output_stream.write(imagedata_buffer, 0, read);
downloaded += read;
publishProgress((int) ((downloaded / filesize) * 100));
}
if (status == DOWNLOADING) {
status = COMPLETE;
}
return BitmapFactory
.decodeStream((InputStream) new ByteArrayInputStream(
byte_output_stream.toByteArray()));
} catch (MalformedURLException excep) {
excep.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return null;
}
}
}
-----------------------------------------------End of Class------------------------------------------------
Here is Xml that i have used for Activity.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_vertical" >
<TextView
android:id="@+id/text_data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ImageView
android:id="@+id/loaded_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dip"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:gravity="center"
android:text="@string/btn_start" />
</FrameLayout>
For Work with image to load from Internet you need to give two permission in your's android project Manifest .And Here are two permission.
This Permission Give us to access internet from our android application.
<uses-permission android:name="android.permission.INTERNET"/>
And Now Second one i have used for To get State of our Android Mobile it is connected to internet or not,and this permission also need to get any changes in network state .
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>