Android客户端发送图像通过socket后接收到黑屏
创始人
2024-10-08 16:34:28
0

问题可能出在客户端和服务器不一致的图像格式上。可以通过以下方式在客户端处理图像:

  1. 加载图像 Bitmap bitmap = BitmapFactory.decodeFile(filePath); // 或者 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample_image);

  2. 转化为字节数组 ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] byteArray = stream.toByteArray();

  3. 发送字节数组到服务器 OutputStream outputStream = socket.getOutputStream(); outputStream.write(byteArray, 0, byteArray.length); outputStream.flush();

在服务器端,可以按照以下方式接收和显示图像:

  1. 接收字节数组 InputStream inputStream = socket.getInputStream(); byte[] buffer = new byte[1024]; int len; ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); }

  2. 转换并显示图像 byte[] byteArray = outputStream.toByteArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); imageView.setImageBitmap(bitmap);

需要注意的是,客户端和服务器端需要使用相同的压缩格式(在本例中为JPEG),否则可能会出现图像显示不一致或者黑屏的问题。

相关内容

热门资讯

现有说明如下!aapoker辅... 现有说明如下!aapoker辅助工具安全吗,wepoker分析(透视)科普教程(新版有挂)-哔哩哔哩...
反观!wpk模拟器是什么,we... 反观!wpk模拟器是什么,wepoker辅助软件视频(透视)有挂教程(有挂详细)-哔哩哔哩1、wep...
事发当天!wepoker高级辅... 事发当天!wepoker高级辅助,cloudpoker辅助(透视)了解教程(有挂详细)-哔哩哔哩1、...
透视有挂!来玩app破解版,哈... 透视有挂!来玩app破解版,哈糖大菠萝能开挂吗(透视)曝光教程(有挂方法)-哔哩哔哩1、这是跨平台的...
据统计!wepoker怎么破解... 据统计!wepoker怎么破解游戏,wepoker正确养号方法(透视)必备教程(揭秘有挂)-哔哩哔哩...
据报道!wepoker插件程序... 据报道!wepoker插件程序激活码,aapoker透视怎么用(透视)曝光教程(有挂工具)-哔哩哔哩...
透视ai!wepoker私人局... 透视ai!wepoker私人局开挂视频,sohoo poker辅助(透视)了解教程(有挂方法)-哔哩...
辅助透视!wepokerplu... 辅助透视!wepokerplus到底是挂了吗,werplan免费挂下载(透视)推荐教程(发现有挂)-...
截至目前!aapoker透视怎... 您好,aapoker透视怎么用这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...
透视好牌!aapoker公共底... 透视好牌!aapoker公共底牌,wepoker好友房开挂(透视)总结教程(有挂工具)-哔哩哔哩1、...