android asynctask 图片,Android 使用AsyncTask 下载图片的例子,学会使用AsyncTask

48304ba5e6f9fe08f3fa1abda7d326ab.png

1 package com.example.loadimgfrominternet;

2

3 import java.io.IOException;

4 import java.io.InputStream;

5 import java.io.OutputStream;

6 import java.net.HttpURLConnection;

7 import java.net.MalformedURLException;

8 import java.net.URL;

9 import java.net.URLConnection;

10

11 import android.app.Activity;

12 import android.content.Context;

13 import android.graphics.Bitmap;

14 import android.graphics.BitmapFactory;

15 import android.os.AsyncTask;

16 import android.os.Bundle;

17 import android.os.SystemClock;

18 import android.view.View;

19 import android.view.View.OnClickListener;

20 import android.widget.Button;

21 import android.widget.ImageView;

22 import android.widget.ProgressBar;

23

24 public class MainActivity extends Activity implements OnClickListener {

25

26 private Button btnGetImg;

27 private Button btnAbort;

28 private ProgressBar mProgressBar;

29 private ImageView mImageView;

30 private static final String ImageUrl = "https://images0.cnblogs.com/i/169207/201408/112229149526951.png";

31

32 ImageLoader loader = null;

33

34 @Override

35 protected void onCreate(Bundle savedInstanceState) {

36 super.onCreate(savedInstanceState);

37 setContentView(R.layout.activity_main);

38 initView();

39 }

40

41 private void initView() {

42 btnGetImg = (Button) findViewById(R.id.btnGetImg);

43 btnAbort = (Button) findViewById(R.id.btnAbort);

44 mProgressBar = (ProgressBar) findViewById(R.id.mProgressBar);

45 btnGetImg.setOnClickListener(this);

46 btnAbort.setOnClickListener(this);

47

48 mProgressBar.setVisibility(View.INVISIBLE);

49 mImageView = (ImageView) findViewById(R.id.mImageView);

50 }

51

52 @Override

53 public void onClick(View v) {

54 int id = v.getId();

55 if (id == R.id.btnGetImg) {

56 loader = new ImageLoader();

57 loader.execute(ImageUrl);

58 btnGetImg.setEnabled(false);

59 } else if (id == R.id.btnAbort) {

60 loader.cancel(true);

61 btnGetImg.setEnabled(true);

62 }

63 }

64

65 class ImageLoader extends AsyncTask {

66 @Override

67 protected void onPreExecute() {

68 mProgressBar.setVisibility(View.VISIBLE);

69 mProgressBar.setProgress(0);

70 mImageView.setImageResource(R.drawable.noimg);

71 super.onPreExecute();

72 }

73

74 @Override

75 protected Bitmap doInBackground(String... params) {

76 String imgUri = params[0];

77

78 try {

79 URL url = null;

80 HttpURLConnection conn = null;

81 InputStream inputStream = null;

82 OutputStream outputStream = null;

83 String filename = "local_temp_image";

84

85 try {

86

87 url = new URL(imgUri);

88 conn = (HttpURLConnection) url.openConnection();

89 conn.setDoInput(true);

90 conn.setDoOutput(false);

91 conn.setConnectTimeout(20 * 1000);

92 inputStream = conn.getInputStream();

93 outputStream = openFileOutput(filename,

94 Context.MODE_PRIVATE);

95 byte[] data = new byte[1024];

96 int seg = 0;

97 long total = conn.getContentLength();

98 long current = 0;

99 while (!isCancelled()

100 && (seg = inputStream.read(data)) != -1) {

101 outputStream.write(data, 0, seg);

102 current += seg;

103 int progress = (int) ((float) current / total * 100);

104 publishProgress(progress);// // 通知进度条UI更新

105 SystemClock.sleep(1000);

106 }

107 } finally {

108 if (conn != null) {

109 conn.disconnect();

110 }

111 if (inputStream != null) {

112 inputStream.close();

113 }

114 if (outputStream != null) {

115 outputStream.close();

116 }

117 }

118 // return BitmapFactory.decodeStream(HandlerData(ImageUrl));

119 return BitmapFactory.decodeFile(getFileStreamPath(filename)

120 .getAbsolutePath());

121 } catch (MalformedURLException e) {

122 e.printStackTrace();

123 } catch (IOException e) {

124 e.printStackTrace();

125 }

126 return null;

127 }

128

129 @Override

130 protected void onProgressUpdate(Integer... values) {

131 if (isCancelled())

132 return;

133 mProgressBar.setProgress(values[0]);

134 btnGetImg.setText(values[0] + "%");

135 super.onProgressUpdate(values);

136 }

137

138 @Override

139 protected void onPostExecute(Bitmap bitmap) {

140 if (bitmap != null) {

141 mImageView.setImageBitmap(bitmap);

142 }

143 mProgressBar.setVisibility(View.INVISIBLE);

144 mProgressBar.setProgress(0);

145 btnAbort.setEnabled(false);

146 super.onPostExecute(bitmap);

147 }

148 }

149

150 /* 根据URI地址读取输入流 */

151 public static InputStream HandlerData(String url) {

152 InputStream inStream = null;

153

154 try {

155 URL feedUrl = new URL(url);

156 URLConnection conn = feedUrl.openConnection();

157 conn.setConnectTimeout(10 * 1000);

158 inStream = conn.getInputStream();

159 } catch (Exception e) {

160 e.printStackTrace();

161 }

162

163 return inStream;

164 }

165 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值