Android图片查看器(Bitmap)

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值