Tuesday, August 2, 2011

Download Image from URL in Android

Some time we need to download Image from Internet (specific URL)..
how can we achieve this i am going to write step by step.

We will download image from internet and show on Image View


-------------------------------------------
App Name: DownloadImageFromURL
Package Name: com.rdc
Android SDK: Android SDK 2.2 / API 8
Default Activity Name: DownloadImageFromURL
-------------------------------------------


DownloadImageFromURL.java
package com.rdc;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

public class DownloadImageFromURL extends Activity {

 private ImageView img = null;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  Bitmap bitmap = DownloadImage
 ("http://www.streetcar.org/streetcars/uploads/560px/560-130.png");

  img = (ImageView) findViewById(R.id.imageView);
  img.setImageBitmap(bitmap);
 }

 private Bitmap DownloadImage(String URL) {
  Bitmap bitmap = null;
  InputStream in = null;
  try {
   in = OpenHttpConnection(URL);
   bitmap = BitmapFactory.decodeStream(in);
   in.close();
  } catch (IOException e1) {
   Toast.makeText(this, e1.getLocalizedMessage(), 
                                       Toast.LENGTH_SHORT)
     .show();
   e1.printStackTrace();
  }
  return bitmap;
 }

 private InputStream OpenHttpConnection(String urlString) 
                      throws IOException {
  InputStream in = null;
  int response = -1;

  URL url = new URL(urlString);
  URLConnection conn = url.openConnection();

  if (!(conn instanceof HttpURLConnection))
   throw new IOException("Not and Http connection");
  try {
   HttpURLConnection httpConn = (HttpURLConnection) conn;
   httpConn.setAllowUserInteraction(false);
   httpConn.setInstanceFollowRedirects(true);
   httpConn.setRequestMethod("GET");
   httpConn.connect();

   response = httpConn.getResponseCode();
   if (response == HttpURLConnection.HTTP_OK) {
    in = httpConn.getInputStream();
   }
  } catch (Exception ex) {
   throw new IOException("Error Connectiong");
  }
  return in;
 }
}

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:weightSum="1">
 <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_weight="0.04"
  android:text="The image downlaoded from Internet" >
  </TextView>
 <ImageView
  android:layout_height="wrap_content"
  android:src="@drawable/icon"
  android:layout_width="match_parent"
  android:layout_weight="0.35"
  android:id="@+id/imageView">
  </ImageView>
</LinearLayout>


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

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

 <application
  android:icon="@drawable/icon"
  android:label="@string/app_name">
  <activity
   android:name=".DownloadImageFromURL"
   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>

The output Screen will be like this..


You can download the complete source code zip file here : DownloadImageFromURL

 cheers!!

 I'd love to hear your thoughts!


No comments:

Post a Comment