/**
* 加载图片类,显示大图
*/
public class NormalLoadPictrue {
private String uri;
private ImageView imageView;
private byte[] picByte;
public void getPicture(String uri, ImageView imageView) {
this.uri = uri;
this.imageView = imageView;
new Thread(runnable).start();
}
@SuppressLint("HandlerLeak")
Handler handle = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
if (picByte != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(picByte, 0, picByte.length);
imageView.setImageBitmap(bitmap);
ProgressDialogUtils.dismissProgressBar();
}
}
}
};
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(10000);
if (conn.getResponseCode() == 200) {
InputStream fis = conn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int length = -1;
while ((length = fis.read(bytes)) != -1) {
bos.write(bytes, 0, length);
}
picByte = bos.toByteArray();
bos.close();
fis.close();
Message message = new Message();
message.what = 1;
handle.sendMessage(message);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
}
加载图片类
最新推荐文章于 2021-04-18 00:52:37 发布