Tuesday, 16 April 2013

Image Download from Internet

Demo:

1)Activity:

package com.venky.imagedownloadingininternet;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {
      private String url = "http://1.bp.blogspot.com/-AzDufr1aELo/UQkn9JcBY_I/"
                  + "AAAAAAAACFY/N9OK59-VLgg/s640/2013-01-26+08.47.29.jpg";

      ImageView iv;

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

            iv = (ImageView) findViewById(R.id.imageView1);

            ImgDownload img = new ImgDownload();
            img.execute();

      }

      class ImgDownload extends AsyncTask<Void, Void, Void> {

            private ProgressDialog pd;
            private Bitmap image;

            @Override
            protected void onPreExecute() {
                  // TODO Auto-generated method stub

                  pd = ProgressDialog.show(MainActivity.this, "Downloading",
                              "Please Wait...");

                  super.onPreExecute();
            }

            @Override
            protected Void doInBackground(Void... params) {
                  // TODO Auto-generated method stub

                  try {
                        URL url1 = new URL(url);

                        URLConnection con = url1.openConnection();

                        InputStream is = con.getInputStream();

                        image = BitmapFactory.decodeStream(is);

                  } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                  }

                  return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                  // TODO Auto-generated method stub

                  pd.cancel();

                  iv.setImageBitmap(image);
            }
      }

}

2)xml:

<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=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/checkBox1"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="156dp"
        android:layout_toRightOf="@+id/checkBox1" />

</RelativeLayout>

3)Download this Project Click Here

No comments:

Post a Comment