Android图片查看器(Bitmap)
来源:互联网 发布:二次型化为矩阵 编辑:程序博客网 时间:2024/05/22 07:07
public class MainActivity extends AppCompatActivity { private EditText et_path; private ImageView iv; //创建handle对象// private Handler handler = new Handler() {// //处理消息// public void handdleMessge(Message.msg) {// Bitmap bitmap = (Bitmap) msg.obj;// iv.setImageBitmap(bitmap);// }// }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1 找到我们关心的控件 et_path = (EditText) findViewById(R.id.et_path); iv = (ImageView) findViewById(R.id.iv); StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy( new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build()); } //2 点击按钮进行查看 指定路径的源码 public void click(View v) { new Thread() { public void run() { try { String path = et_path.getText().toString().trim(); File file = new File(getCacheDir(), Base64.encodeToString(path.getBytes(), Base64.DEFAULT)); if (file.exists() && file.length()>0) { //使用缓存的图片 Log.d("mylog", "使用缓存图片"); final Bitmap cacheBitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); // 把cacheBitmap 显示到iv上 // Message msg = new Message().obtain(); // msg.obj = cacheBitmap; // handler.sendMessage(msg); // 发消息 runOnUiThread(new Runnable() { @Override public void run() { iv.setImageBitmap(cacheBitmap); } }); } else { //第一次访问 联网获取数据 Log.d("mylog", "第一次访问连接网络"); //2.2 创建url对象 URL url = new URL(path); //2.3 获取httpurlconnection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //2.4 设置请求的方式 conn.setRequestMethod("GET"); //2.5 设置超时时间 Log.d("mylog", "超时之前"); conn.setConnectTimeout(5000); Log.d("mylog", "超时之后"); //2.6 获取服务器返回的状态码 int code = conn.getResponseCode(); Log.d("mylog", "得到状态码"); if (code == 200) { Log.d("mylog", "code=200"); //2.7 获取图片的数据 不管是什么数据 txt、image 都是以流的形式返回 InputStream in = conn.getInputStream(); //2.7 缓存图片 谷歌给我们提供了一个缓存目录 FileOutputStream fos = new FileOutputStream(file); int len = -1; byte[] buffer = new byte[1024]; // 1kb while ((len = in.read(buffer)) != -1) { fos.write(buffer, 0, len); } fos.close(); in.close(); //2.8 通过位图工厂 获取bitmap (bitmap) final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); //这句api 不管你在什么位置上调用 action都运行在ui线程里 runOnUiThread(new Runnable() { @Override public void run() { // run方法一定执行在ui线程里 //2.9 把bitmap显示到tv上 iv.setImageBitmap(bitmap); } });// Message msg = Message.obtain(); //使用msg的静态方法 可以减少对象的创建// msg.obj = bitmap;// handler.setMessage(msg); // 发消息 } } } catch (Exception e) { e.printStackTrace(); } } }.start(); }}
public class StreamTools { //把一个ImputStream 转换成一个String public static String readStream(InputStream in) throws IOException { //定义一个内存输出流 ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len = -1; byte[] buffer = new byte[1024]; // 1kb while((len=in.read(buffer)) != -1) { baos.write(buffer, 0, len); } in.close(); String content = new String(baos.toByteArray()); return content; }}
中途报了一个异常,大概是无法解析网页的意思,其实是连不上网的原因,我联网之后继续报异常,重启了一下模拟器就好了
2 0
- Android图片查看器(Bitmap)
- Android笔记十九.Bitmap之简易图片查看器
- 网络图片查看器HttpURLConnection+Bitmap
- Android图片查看器
- Android 图片查看器
- Android图片查看器
- Android图片查看器
- Android bitmap图片处理
- android bitmap压缩图片
- Android bitmap图片处理
- android bitmap 图片处理
- android 压缩图片bitmap
- android bitmap图片压缩
- Android bitmap图片处理
- Android bitmap图片处理
- Android bitmap图片处理
- Android bitmap图片处理
- Android bitmap合成图片
- 第二本书unit1
- Xposed
- Java字节码浅析(三)循环语句
- 欢迎使用CSDN-markdown编辑器
- 数值作业:二分法求方程的根之C语言实现代码
- Android图片查看器(Bitmap)
- 超级楼梯hdu2041
- 你不知道的 JavaScript 错误和调用栈常识
- 最大上升子序列和
- day4:不知道完成情况的作业
- eclipse 文件的相对路径是对project而言
- 在虚拟机上安装Python3.5.1
- Vue项目 一、准备以及初始项目解析
- mysql数据库中多表关联查询的实例