安卓图片加载代码
来源:互联网 发布:春英广场舞网络一线牵 编辑:程序博客网 时间:2024/05/17 01:07
1.大框架(简单界面)
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new Thread() { @Override public void run() { byte[] a = new byte[0]; // 获取图片数据 try { a = Imageservice.getImgae(editText.getText().toString()); } catch (IOException e) { e.printStackTrace(); } Bitmap bitmap = BitmapFactory.decodeByteArray(a, 0, a.length);//数据转化为图片 Message message = Message.obtain(); message.what = 0x11; handler.sendMessage(message); message.obj = bitmap; } }.start(); }});handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0x11) { imageView.setImageBitmap((Bitmap) msg.obj); } }};2.Imageservice类
3.SteamTool工具类(负责从流中读取数据)public static byte[] getImgae(String string) throws IOException { URL url = new URL(string); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 基于HTTP协议的连接对象 conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); InputStream inputstream = conn.getInputStream(); try { return StreamTool.readData(inputstream); // 读取流中数据并返回 // 下列操作获取源码 /*String html=new String(StreamTool.readData(inputstream),"UTF-8"); return html;*/ } catch (Exception e) { e.printStackTrace(); } return null;}
public class StreamTool { public static byte[] readData(InputStream inputstream) throws Exception { ByteArrayOutputStream bb = new ByteArrayOutputStream(); //可以捕获内存缓冲区的数据,转换成字节数组。 byte[] b = new byte[1024]; int len = 0; while ((len = inputstream.read(b, 0, b.length)) != -1) { bb.write(b, 0, len); } return bb.toByteArray(); ////获取内存缓冲中的数据 }}
阅读全文
0 0
- 安卓图片加载代码
- 安卓代码优化 使用ImageLoader实现图片异步加载
- 安卓 textview 加载图片
- 安卓开发-加载图片
- 安卓加载大量图片
- 安卓图片加载库
- 安卓加载本地图片
- 安卓Glide加载图片
- 【安卓笔记】异步加载大量图片
- 安卓图片的缓存与加载
- 安卓处理Html----图片异步加载
- 安卓图片加载框架的学习
- 安卓图片加载文字模糊解决方法
- 安卓开发:Volley加载图片
- 安卓GridView异步加载网络图片
- 【安卓笔记】异步加载大量图片
- 安卓AsyncTask异步加载图片
- 安卓如何加载GIF图片
- Android View的onTouchEvent和OnTouch区别
- 消除重复数字
- git使用教程
- 引用(交换数值)
- css其他属性
- 安卓图片加载代码
- Action参数封装过程中,数据类型转换问题
- IPC-共享内存
- PHP 单例模式解析和实战
- Spark优化--开发调优
- HDU 3518Boring counting(后缀数组)
- Luogu 1351(树形dp)
- Spark cache和checkpoint
- Unity 碰撞器与触发器