How do i send File object while emit using socket.io with node.js in android?
I am using this library to send media using socket.io with node.js in my android app chat module.
right now i am using byte[] and also tried by base64 using below code.
Using Base64
Send:
socket.emit("image", base64 string);
Retrieve:
socket.on("image", new Emitter.Listener() {
@Override
public void call(final Object... arg0) {
runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(getBitmapFromBase64(arg0[0].toString()));
}
});
}
});
Using Byte Array
Send:
socket.emit("image", byte[] of media);
Retrieve:
socket.on("image", new Emitter.Listener() {
@Override
public void call(final Object... arg0) {
runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(getBitmapFromByteArray((byte[])arg0[0]));
}
});
}
});
All media file sharing is working fine in both of above case.
But can i send whole File Object while emit to node? like
Send:
socket.emit("image", File Object);
i.e,
File file = new File(path);
socket.emit("image", file);
Retrieve:
socket.on("image", new Emitter.Listener() {
@Override
public void call(final Object... arg0) {
**arg0[0] as a file object**
}
});
Is it possible?
Can i send any Object or Custom object while emit to Node.js from android?
Unfortunately, you can't, Please convert objects to JSON or byte arrays before sending data.
@addonajay You must send the image to the server, and then to send the url per socket.
look at this example: https://medium.com/@Mewsse/file-upload-with-socket-io-9d2d1229494