HttpURLConnection请求图片
来源:互联网 发布:.market域名 编辑:程序博客网 时间:2024/05/29 05:08
效果图:
页面代码:
public class HttpJpgActivity extends Activity { private ImageView view; private Handler handler; private File file = new File(Environment.getExternalStorageDirectory(), "baidu.jpg"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.item_image); view = (ImageView) findViewById(R.id.iv_city); new Thread(new ImageThread()).start(); handler = new Handler() { @Override public void handleMessage(Message msg) { try { FileInputStream fileInputStream = new FileInputStream(file); Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream); view.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } }; } public class ImageThread implements Runnable { @Override public void run() { try { URL url = new URL("http://www.baidu.com/img/bdlogo.png"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setConnectTimeout(3000); urlConnection.setReadTimeout(3000); urlConnection.connect(); FileOutputStream fileOutputStream = new FileOutputStream(file); if (urlConnection.getResponseCode() == 200) { InputStream inputStream = urlConnection.getInputStream(); byte[] bytes = new byte[1024]; int len; while ((len = inputStream.read(bytes)) != -1) { fileOutputStream.write(bytes, 0, len); } //将缓冲刷入文件 fileOutputStream.flush(); handler.sendEmptyMessage(0x001); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }}
权限:
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
0 0
- HttpURLConnection请求图片
- HttpURLConnection的POST请求加载数据图片
- Handler+HttpURLConnection请求网络图片,字符串
- HttpURLConnection请求
- HttpURLConnection请求
- HttpURLConnection请求
- HttpURLConnection 请求
- HttpUrlConnection请求
- 网络请求----HttpURLConnection的get,post和图片加载
- 即拿即用-HttpURLConnection分别实现图片,文本,文件的请求
- Android HttpURLConnection获取网络下载图片POST请求方式
- HttpURLConnection获取网络数据(实例:网路请求图片)
- HttpURLConnection请求服务
- HttpURLConnection post 请求
- HttpURLConnection post请求
- 请求网络之HttpUrlConnection
- HttpURLConnection数据请求
- HttpUrlconnection get请求数据
- CSS3动画
- A visual analytics framework for spatio-temporal analysis and modelling
- HDU 5090--Game with Pearls【二分图最大匹配】
- *nux平台上的C10M问题
- Ubuntu之Gitlab、Gerrit、Jenkins协调工作配置
- HttpURLConnection请求图片
- 计算机系统(六)OS 管理 硬件
- 内存管理基础
- 用一个textview显示数字,让数字从一个值渐变到另外一个值
- PowerBuilder 使用JDBC连接MYSQL
- Git Push 避免用户名和密码方法
- SpringMVC jdbc返回的不是标准对象时处理方法
- leetcode Path Sum 路径和
- 一组成绩,一组学生,按照成绩的大小,由高到底排序出姓名和成绩(二叉树)实现