1
+ package com .javatechig .handlerexample ;
2
+
3
+ import java .io .InputStream ;
4
+ import org .apache .http .HttpEntity ;
5
+ import org .apache .http .HttpResponse ;
6
+ import org .apache .http .HttpStatus ;
7
+ import org .apache .http .client .methods .HttpGet ;
8
+ import org .apache .http .impl .client .DefaultHttpClient ;
9
+
10
+ import com .javatechige .handlerexample .R ;
11
+
12
+ import android .app .Activity ;
13
+ import android .app .ProgressDialog ;
14
+ import android .graphics .Bitmap ;
15
+ import android .graphics .BitmapFactory ;
16
+ import android .os .Bundle ;
17
+ import android .os .Handler ;
18
+ import android .os .Message ;
19
+ import android .util .Log ;
20
+ import android .view .View ;
21
+ import android .view .View .OnClickListener ;
22
+ import android .widget .Button ;
23
+ import android .widget .ImageView ;
24
+
25
+ public class MainActivity extends Activity {
26
+
27
+ private ProgressDialog progressDialog ;
28
+ private ImageView imageView ;
29
+ private String url = "http://www.9ori.com/store/media/images/8ab579a656.jpg" ;
30
+ private Bitmap bitmap = null ;
31
+
32
+ public void onCreate (Bundle savedInstanceState ) {
33
+ super .onCreate (savedInstanceState );
34
+ setContentView (R .layout .activity_main );
35
+
36
+ imageView = (ImageView ) findViewById (R .id .imageView );
37
+
38
+ Button start = (Button ) findViewById (R .id .button1 );
39
+ start .setOnClickListener (new OnClickListener () {
40
+
41
+ @ Override
42
+ public void onClick (View arg0 ) {
43
+ progressDialog = ProgressDialog .show (MainActivity .this ,
44
+ "" , "Loading.." );
45
+ new Thread () {
46
+ public void run () {
47
+ bitmap = downloadBitmap (url );
48
+ messageHandler .sendEmptyMessage (0 );
49
+ }
50
+ }.start ();
51
+
52
+ }
53
+ });
54
+ }
55
+
56
+ private Handler messageHandler = new Handler () {
57
+ public void handleMessage (Message msg ) {
58
+ super .handleMessage (msg );
59
+ imageView .setImageBitmap (bitmap );
60
+ progressDialog .dismiss ();
61
+ }
62
+ };
63
+
64
+ private Bitmap downloadBitmap (String url ) {
65
+ // Initialize the default HTTP client object
66
+ final DefaultHttpClient client = new DefaultHttpClient ();
67
+
68
+ //forming a HttoGet request
69
+ final HttpGet getRequest = new HttpGet (url );
70
+ try {
71
+
72
+ HttpResponse response = client .execute (getRequest );
73
+
74
+ //check 200 OK for success
75
+ final int statusCode = response .getStatusLine ().getStatusCode ();
76
+
77
+ if (statusCode != HttpStatus .SC_OK ) {
78
+ Log .w ("ImageDownloader" , "Error " + statusCode +
79
+ " while retrieving bitmap from " + url );
80
+ return null ;
81
+ }
82
+
83
+ final HttpEntity entity = response .getEntity ();
84
+ if (entity != null ) {
85
+ InputStream is = null ;
86
+ try {
87
+ // getting contents from the stream
88
+ is = entity .getContent ();
89
+
90
+ // decoding stream data back into image Bitmap
91
+ final Bitmap bitmap = BitmapFactory .decodeStream (is );
92
+
93
+ return bitmap ;
94
+ } finally {
95
+ if (is != null ) {
96
+ is .close ();
97
+ }
98
+ entity .consumeContent ();
99
+ }
100
+ }
101
+ } catch (Exception e ) {
102
+ getRequest .abort ();
103
+ Log .e (getString (R .string .app_name ), "Error " + e .toString ());
104
+ }
105
+
106
+ return null ;
107
+ }
108
+ }
0 commit comments