Pages

Thursday, June 2, 2016

Downloading Image in Android

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 in.blogspot.kantapp.downloadingimageinandroid;

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;

import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends Activity {
    private String imageURL = "http://vignette2.wikia.nocookie.net/dragonball/images/3/31/Render_Dragon_Ball_goku_M1.png/revision/latest?cb=20150412202653";
    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 {


                URL url = new URL(imageURL);


                URLConnection conn = url.openConnection();


                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="in.blogspot.kantapp.downloadingimageinandroid">
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

No comments:

Post a Comment