【发布时间】:2021-06-09 21:52:09
【问题描述】:
我想通过套接字发送图像,但我无法在 android 中完成,有人可以帮我吗?
System.out.println("iniciooooo");
//converting image to bytes with base64
Bitmap b = BitmapFactory.decodeFile("/sdcard/ajeffer.jpg");
ByteArrayOutputStream byte2= new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG,70,byte2);
byte[] enbytes = byte2.toByteArray();
String bb = Base64.encodeToString(enbytes,Base64.DEFAULT);
System.out.println(Base64.encodeToString(enbytes,Base64.DEFAULT));
data.writeUTF(bb);
FileOutputStream file;
//receiving the image in bytes to convert it into an image
DataInputStream dain = new DataInputStream(s.getInputStream());
msg = dain.readUTF();
File ff = new File("/sdcard/a2jeffer.jpg");
byte[] deco = Base64.decode(dain.readUTF(),Base64.DEFAULT);
Bitmap bit = BitmapFactory.decodeByteArray(deco,0,deco.length);
file = new FileOutputStream(ff);
bit.compress(Bitmap.CompressFormat.JPEG,70,file);
//the image is not created
【问题讨论】:
-
你不需要这一切。
writeUTF()只能发送 64K 个字符,无论如何 Base-64 编码毫无意义。如果您确实必须进一步压缩,只需发送原始图像或压缩程度更高的图像的字节即可。您也不需要接收端的 BitmapFactory。两端只是标准的 Java 复制循环。 -
我理解,我认为你是对的,我发现我必须从字符串中获取字节,但我没有设法将它保存为图像,你能给我一些例子吗代码如果不是太麻烦的话
-
你不需要这个字符串。只需直接复制字节即可。这一切我都已经说了。