Download Image from online

By ukmodak | March 31st 2024 10:30:09 AM | viewed 734 times

create a main.xml layout and Add a button to download image in the location: layout(android studio)

<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="426dp" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="Welcome to main activity "
                android:textColor="#ffffff"
                android:textSize="15sp" />

           <Button
                android:id="@+id/btnDownloadImage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/btnAnimation"
                android:layout_toRightOf="@+id/btnAnimation"
                android:text="Download an image from online"
                android:textSize="8sp" />
          

        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

create a ProjectonOverallClassMainActivity activity to download image

package com.uk.modak;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ProjectonOverallClassMainActivity extends Activity {
   
	
	public Button btnDownloadImage;
		
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
               
         btnDownloadImage = (Button) findViewById(R.id.btnDownloadImage);           
         btnDownloadImage.setOnClickListener(new View.OnClickListener() {
    		
			public void onClick(View v) {				
				Intent d = new Intent(ProjectonOverallClassMainActivity.this,DownloadImage.class);
				startActivity(d);
			}
      });                      
    }
    
    public void onStart(){
    	super.onStart();
    	 	
    }
      
    
}

create a imagedownloadlayout.xml layout to download image in the location: layout(android studio)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#B0C4DE"
    android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageId"
            android:layout_width="wrap_content"
            android:layout_height="400dp"
            android:layout_marginTop="50dp"
            android:layout_gravity="center"/>

</LinearLayout>

create an activity in the location: src/main/java/com/uk/modak/DownloadImage (android studio)


package com.uk.modak;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;

public class DownloadImage extends Activity {

	String UR = "http://www.androidbegin.com/wp-content/uploads/2013/07/HD-Logo.gif";
    //String UR = "http://www.ukmodak.com/public/images/uklogo.png";
	ImageView imageId;
	ProgressDialog mProgressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.imagedownloadlayout);

		imageId = (ImageView) findViewById(R.id.imageId);

		new DownloadImageFromOnline().execute(UR);

	}


	private class DownloadImageFromOnline extends AsyncTask {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();

			mProgressDialog = new ProgressDialog(DownloadImage.this);

			mProgressDialog.setTitle("Download Image Tutorial");

			mProgressDialog.setMessage("Loading...");
			mProgressDialog.setIndeterminate(false);

			mProgressDialog.show();
		}

		@Override
		protected Bitmap doInBackground(String... URL) {

			String imageURL = URL[0];

			Bitmap b = null;

			try {

				URL url = new URL(imageURL);


				Log.d("MYTAG", "Connecting");


				HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
				httpURLConnection.setReadTimeout(10000 /* milliseconds */);
				httpURLConnection.setConnectTimeout(15000 /* milliseconds */);
				httpURLConnection.setRequestMethod("GET");
				httpURLConnection.connect();
				Log.d("MYTAG", httpURLConnection.getResponseMessage());
				InputStream is = httpURLConnection.getInputStream();


				final BitmapFactory.Options options = new BitmapFactory.Options();
				options.inSampleSize = 1;
				b= BitmapFactory.decodeStream(is, null, options);


			} catch (Exception e) {

				e.printStackTrace();
			}

			return b ;

		}

		@Override
		protected void onPostExecute(Bitmap result) {

			mProgressDialog.dismiss();

			imageId.setImageBitmap(result);


		}
	}

}

to to menifeast and setup internet permission

  
    

to to menifeast and setup activity that we use

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".ProjectonOverallClassMainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        <activity  
            android:name=".DownloadImage"
            android:label="@string/app_name" >
        </activity>
        
        <receiver android:name=".ConnectionReceive">
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            </intent-filter>
        </receiver>
        
    </application>
bONEandALL
Visitor

Total : 26654

Today :3

Today Visit Country :

  • France
  • United States