Friday, 21 June 2013

Download Image from Server(url)

main.xml


<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button android:text="Click to start download" android:onClick="downloadPicture"
            android:layout_height="wrap_content" android:layout_width="wrap_content" />
        <ImageView  android:id="@+id/imageView1"
            android:layout_height="match_parent" android:layout_width="match_parent"></ImageView>
    </LinearLayout>


Java File

package com.down;

    import java.io.IOException;
     
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.StatusLine;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpUriRequest;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;


     
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.view.View;
import android.widget.ImageView;
     
    public class DownimageActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
     
        }
     
        public void downloadPicture(View view) {
            final ProgressDialog dialog = ProgressDialog.show(this, "Download",
                    "downloading");
            dialog.show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        final Bitmap downloadBitma = downloadBitmap("http://www.ipottechnologies.com/android/Penguins.jpg");
                        final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                imageView.setImageBitmap(downloadBitma);
                            }
                        });
     
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        dialog.dismiss();
                    }
                }
            }).start();
     
        }
     
        private Bitmap downloadBitmap(String url) throws IOException {
            HttpUriRequest request = new HttpGet(url.toString());
            HttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(request);
     
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                byte[] bytes = EntityUtils.toByteArray(entity);
     
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0,
                        bytes.length);
                return bitmap;
            } else {
                throw new IOException("Download failed, HTTP response code "
                        + statusCode + " - " + statusLine.getReasonPhrase());
            }
        }
    }


Android Manifest File

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

No comments:

Post a Comment