【发布时间】:2014-11-19 06:13:54
【问题描述】:
我正在尝试将我的相机在我的应用程序中拍摄的图像作为字节数组发送。我将位图转换为字节数组,然后转换为 Base64 字符串,然后将其编码为字节数组并发送。但是,当我尝试发送它时,我收到一个异常,即我的字符串太长而无法发送。
准备发送到套接字:
if(currentChatPartner==null)
return;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
msg.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, 0);
sendThroughSocketImage(encoded, clientSocket, currentChatPartner.getIP(), currentChatPartner.getPort());
通过套接字发送:
byte[] decoded = null;
decoded = encoded.getBytes();
try
{
socket.send(new DatagramPacket(decoded, decoded.length, ip, port));
} catch (IOException e)
{
System.err.println("\nUnable to send message");
return false;
}
return true;
有时它可以工作,但大多数情况下它没有:(这是有原因的吗?有没有办法可以修复它或缩短要发送的字符串??
【问题讨论】:
标签: java android arrays sockets bitmap