Android InputStream转Bitmap
android socket服务端 接收Delphi socket客户端发来的图片,保存到bitmap中,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public static Bitmap readInputStreamToBitmap(InputStream ins, int fileSize) { if (ins == null ) { return null ; } byte [] b; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { byte [] buffer = new byte [ 1024 ]; int size = - 1 ; int len = 0 ; // 已经接收长度 size = ins.read(buffer); while (size != - 1 ) { len = len + size; // bos.write(buffer, 0 , size); if (fileSize == len) { // 接收完毕 break ; } size = ins.read(buffer); } b = bos.toByteArray(); bos.close(); } catch (IOException e) { e.printStackTrace(); return null ; } if (b.length != 0 ) { return BitmapFactory.decodeByteArray(b, 0 , b.length); } return null ; } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步