将读取的图片的InputStream流转为字节流

来源:互联网 发布:东北农大网络教育 编辑:程序博客网 时间:2024/05/21 09:01

http://sizeed.blog.163.com/blog/static/96525451201152292810544/


public static byte[] getByte(String fileName) {
        byte[] bt = null;
        FileInputStream stream = null;
        ByteArrayOutputStream out = null;
        try {
            stream = new FileInputStream(fileName);
            out = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int len = 0;
            while ((len = stream.read(b)) != -1) {
                out.write(b, 0, len);
            }
            stream.close();
            out.close();
            bt = out.toByteArray();
            Log.i("byteArray", ""+bt);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                if (stream != null)
                    stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            try {
                if (out != null)
                    out.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        out = null;
        return bt;
    }

原创粉丝点击