download image in android

Image Download from server to android device example

Image Download from server to android device example

If the image file is in the remote server to get the image file in your device, need to download the image file. So friends today we are going to learn how download image file from server. You understand that it’s need network connection to do this task, so internet permission need to add in the manifest file of the application. In android internet calling should to do under a thread, here we take ‘AsyncTask’(Thread in android) class to do this process.

 

Permission :-

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

Read How Add Permission to android manifest file.

ShortCode :- (Run under thread)

 

URL url = new URL(your image url goes here);

URLConnection urlConnection = url.openConnection();

InputStream input = new BufferedInputStream(urlConnection.getInputStream());

imageBitmap = BitmapFactory.decodeStream(input);


DownloadImageActivity.java


package com.androidfromhome.downloadimage;

import java.io.BufferedInputStream;
import java.io.InputStream;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class DownloadImageActivity extends Activity implements OnClickListener {

    private ImageView iv_image;
    private Button btn_load;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download_image);
        
        iv_image=(ImageView)findViewById(R.id.iv_image);
        btn_load=(Button)findViewById(R.id.btn_load);
        btn_load.setOnClickListener(this);
        
    }
       
       
       public class ImageServerToDeviseTask extends AsyncTask<String, Void, Bitmap> {
            
            private ProgressDialog progDailog;
            String imageUrl="";
            private Bitmap imageBitmap;
            
            public ImageServerToDeviseTask(String imageUrl){
                
            this.imageUrl=imageUrl;
            progDailog=new ProgressDialog(DownloadImageActivity.this);    
            
            }

            
            
            protected void onPreExecute() {
                progDailog.setCancelable(false);
                progDailog.setMessage("Please Wait...");
                progDailog.show();    

            }
            
            @Override
            protected Bitmap doInBackground(String... params) {
                // TODO Auto-generated method stub
                 try {
                    
                       URL url = new URL(imageUrl);
                       URLConnection urlConnection = url.openConnection();
                       InputStream input = new BufferedInputStream(urlConnection.getInputStream());
                      
                       imageBitmap = BitmapFactory.decodeStream(input);
                } catch (Exception e) {
                      e.printStackTrace();
                }
                 
                return imageBitmap;
            }

            
            
            @Override
            protected void onPostExecute(Bitmap result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            
            if (result!=null) {
            iv_image.setImageBitmap(result);    
            }else {
                Toast.makeText(DownloadImageActivity.this, "No Image Found.", Toast.LENGTH_LONG).show();
            }
            progDailog.dismiss();
            
            
            
            }
       }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn_load:
            new ImageServerToDeviseTask("http://androidfromhome.com/json/LOGO1_03.png").execute("");
            break;

        default:
            break;
        }
        
    }
}


activity_download_image.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidfromhome.downloadimage.DownloadImageActivity" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

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

        <FrameLayout
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:layout_margin="2dp" >

            <ImageView
                android:id="@+id/iv_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="6dp"
                android:src="@drawable/ic_launcher" />

        </FrameLayout>

        <Button
            android:id="@+id/btn_load"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="4dp"
            android:background="#6FB35C"
            android:padding="10dp"
            android:text="Load Image"
            android:textColor="#ffffff"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>


Download Full Source Code

 

 

Related Post:-

Simple Alert Dialog in android

Custom Spinner in android

How use html page in android

Splash Screen example in android

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget