Wednesday, 13 March 2013

Downloading Image in Android

ScreenShots:

activity_main.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" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="72dp"
        android:layout_marginTop="74dp"
          />

</RelativeLayout>

MainActivity.java:
package com.ram.downloadimage1;

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 imageURL = "http://www.fnordware.com/superpng/pnggrad8rgb.png";
    private ImageView image;

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

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

        ImgDownload img = new ImgDownload();

        img.execute();

    }

    private class ImgDownload extends AsyncTask {
        ProgressDialog pd;
        private Bitmap pic;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            pd = ProgressDialog.show(MainActivity.this, "downloading Image",
                    "Plz wait");

            super.onPreExecute();
        }

        @Override
        protected Object doInBackground(Object... objects) {
            try {

                // This class can be manipulate URLs of any protocol
                URL url = new URL(imageURL);

                // URLConnection must be configured before it has connected to
                // the remote resource. Instances of URLConnection are not
                // reusable: you must use a different instance for each
                // connection to a resource

                URLConnection conn = url.openConnection();

                // Decode an input stream into a bitmap. If the input stream is
                // null, or cannot be used to decode a bitmap, the function
                // returns null. The stream's position will be where ever it was
                // after the encoded data was read.

                pic = BitmapFactory.decodeStream(conn.getInputStream());
            } catch (Exception ex) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            pd.cancel();
            image.setImageBitmap(pic);
        }
    }
}

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

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.ram.downloadimage1.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>

 

1 comment: