服务器(nodejs)和安卓设备之间的java图像传输太慢
我正在尝试使用socketio在两台设备之间发送图像。我可以发照片,但时间太长了。我使用字节数组进行图像传输。
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
// convert from byte array to bitmap
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
我使用getImage(返回位图)来显示。我使用getBytes(来自位图)进行图像传输
服务器日志(图像字节数组)字节数组流花费的时间太长
这样转移过程就太长了。你知道有没有更快的解决方案?我使用安卓上的GoTox客户端来实现socketio。提前谢谢。。
更新
在安卓中:
Message msg = new Message();
msg.setType("IMAGE");
msg.setImage(DbBitmapUtility.getBytes(bmp));
msg.setSender(userName);
msg.setTo(toName);
Gson gson = new Gson();
String json = gson.toJson(msg);
socket.emit("message", json);
在nodejs服务器中:
socket.on('message', function(message) {
users[obj.to].emit('event', message);
//save to mongodb on nodejs
}
在安卓中接收时:
if (event.toString().equals("event")) {
Type listType = new TypeToken<Message>() {
}.getType();
Gson gson = new Gson();
Message message = gson.fromJson(args[0].toString(), listType);
//conditions
mMessages.add(message);
scrollMyListViewToBottom();//notifydatasetChanged
}
共 (0) 个答案