如何通过socket接收图片并保存到本地

ashelyll 2017-10-09 04:58:32
现在项目中有个需求是通过socket接收网关端发来的图片并保存至手机存储中,由于对socket通信不甚了解,希望熟悉的朋友们能提供一下通过socket接收图片的参考代码,十分感谢!
...全文
561 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
跟通过Socket传输个字符串是一样的道理;只是这是字节流。 服务端Socket读取文件字节流,并传到客户端,客户端再写字节流到对应文件就可以了;这样用,最好再加个MD5验证,以保证传输的完整性
ashelyll 2017-10-11
  • 打赏
  • 举报
回复
引用 1 楼 boise 的回复:
/** * return a bitmap from service * * @param url * @return bitmap type */ public final static Bitmap returnBitMap(String url) { URL myFileUrl = null; Bitmap bitmap = null; try { myFileUrl = new URL(url); HttpURLConnection conn; conn = (HttpURLConnection) myFileUrl.openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; }
引用 1 楼 boise 的回复:
/** * return a bitmap from service * * @param url * @return bitmap type */ public final static Bitmap returnBitMap(String url) { URL myFileUrl = null; Bitmap bitmap = null; try { myFileUrl = new URL(url); HttpURLConnection conn; conn = (HttpURLConnection) myFileUrl.openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; }
这个是http请求吧,我需要的是socket tcp通信,不过还是谢谢了~
ganshenml 2017-10-11
  • 打赏
  • 举报
回复
一样的,换成接收字节流然后转换成bitmap及图片即可
ashelyll 2017-10-11
  • 打赏
  • 举报
回复
引用 3 楼 cftxlin 的回复:
跟通过Socket传输个字符串是一样的道理;只是这是字节流。 服务端Socket读取文件字节流,并传到客户端,客户端再写字节流到对应文件就可以了;这样用,最好再加个MD5验证,以保证传输的完整性
谢谢~
bjgxjob 2017-10-10
  • 打赏
  • 举报
回复
/** * return a bitmap from service * * @param url * @return bitmap type */ public final static Bitmap returnBitMap(String url) { URL myFileUrl = null; Bitmap bitmap = null; try { myFileUrl = new URL(url); HttpURLConnection conn; conn = (HttpURLConnection) myFileUrl.openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; }

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧