首先在AndroidMainifest中添加上網權限html
1
|
<uses-permission android:name=
"android.permission.INTERNET"
></uses-permission>
|
佈局文件activity_main.xmljava
Activity代碼:android
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
public
class
MainActivity
extends
Activity{
private
Button button;
private
ImageView imageView;
private
ProgressDialog progressDialog;
private
final
String IMATH_PATH = http:
//image16-c.poco.cn/best_pocoers/20141010/11092014101016572228935421.jpg;
private
AsyncTask<string,> task;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn);
imageView = (ImageView)findViewById(R.id.img);
progressDialog =
new
ProgressDialog(
this
);
progressDialog.setTitle(提示信息);
progressDialog.setMessage(正在下載中,請稍後);
progressDialog.setOnCancelListener(
new
OnCancelListener() {
@Override
public
void
onCancel(DialogInterface arg0) {
// TODO Auto-generated method stub
task.cancel(
true
);
}
});
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
button.setOnClickListener(
new
OnClickListener() {
@Override
public
void
onClick(View arg0) {
// TODO Auto-generated method stub
task =
new
MyAsyncTask().execute(IMATH_PATH);
}
});
}
class
MyAsyncTask
extends
AsyncTask<string,>{
@Override
protected
void
onPreExecute() {
// TODO Auto-generated method stub
super
.onPreExecute();
progressDialog.setProgress(
0
);
progressDialog.show();
}
@Override
protected
void
onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super
.onProgressUpdate(values);
progressDialog.setProgress(values[
0
]);
}
@Override
protected
byte
[] doInBackground(String... params) {
// TODO Auto-generated method stub
HttpClient httpClient =
new
DefaultHttpClient();
HttpGet httpGet =
new
HttpGet(params[
0
]);
byte
[] image =
new
byte
[]{};
try
{
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream inputStream =
null
;
ByteArrayOutputStream byteArrayOutputStream =
new
ByteArrayOutputStream();
if
(httpEntity !=
null
&& httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
long
file_length = httpEntity.getContentLength();
long
total_length =
0
;
int
length =
0
;
byte
[] data =
new
byte
[
1024
];
inputStream = httpEntity.getContent();
while
(-
1
!= (length = inputStream.read(data))){
total_length += length;
byteArrayOutputStream.write(data,
0
, length);
int
progress = ((
int
)(total_length/(
float
)file_length)*
100
);
publishProgress(progress);
}
}
image = byteArrayOutputStream.toByteArray();
inputStream.close();
byteArrayOutputStream.close();
}
catch
(Exception e){
e.printStackTrace();
}
finally
{
httpClient.getConnectionManager().shutdown();
}
return
image;
}
@Override
protected
void
onPostExecute(
byte
[] result) {
// TODO Auto-generated method stub
super
.onPostExecute(result);
Bitmap bitmap = BitmapFactory.decodeByteArray(result,
0
, result.length);
imageView.setImageBitmap(bitmap);
progressDialog.dismiss();
}
}
}
</string,></string,>
|
講解:編程
syncTask:異步任務,從字面上來講,就是在咱們的UI主線程運行的時候,異步的完成一些操做。AsyncTask容許咱們的執行一個異步的任務在後臺。咱們能夠將耗時的操做放在異步任務當中來執行,並隨時將任務執行的結果返回給咱們的UI線程來更新咱們的UI控件。經過AsyncTask咱們能夠輕鬆的解決多線程之間的通訊問題。網絡
怎麼來理解AsyncTask呢?通俗一點來講,AsyncTask就至關於Android給咱們提供了一個多線程編程的一個框架,其介於Thread和Handler之間,咱們若是要定義一個AsyncTask,就須要定義一個類來繼承AsyncTask這個抽象類,並實現其惟一的一個 doInBackgroud 抽象方法。要掌握AsyncTask,咱們就必需要一個概念,總結起來就是: 3個泛型,4個步驟。多線程
3個泛型指的是什麼呢?咱們來看看AsyncTask這個抽象類的定義,當咱們定義一個類來繼承AsyncTask這個類的時候,咱們須要爲其指定3個泛型參數:框架
1
|
AsyncTask <params, result=
""
></params,>
|
咱們在定義一個類繼承AsyncTask類的時候,必需要指定好這三個泛型的類型,若是都不指定的話,則都將其寫成Void,例如:異步
1
|
AsyncTask <
void
,
void
=
""
></
void
,>
|
4個步驟:當咱們執行一個異步任務的時候,其須要按照下面的4個步驟分別執行ide
結伴旅遊,一個免費的交友網站:www.jieberu.com佈局
推推族,免費得門票,遊景區:www.tuituizu.com