I'm making a code and I want to send a mp4 file to another Android device. I have reached to connect both Androids via Wifi and write from one a simple for cycle from 1-20 and the other Android device reads and displays the number that is sent.
Here it is the interesting part of the "sender":
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
Log.d("ClientActivity", "C: Connecting...");
Socket socket = new Socket(serverAddr, port);
connected = true;
while (connected) {
try {
Log.d("ClientActivity", "C: Sending command.");
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
.getOutputStream())), true);
for (int i = 1; i < 20; i++) {
out.println(i);
i=i++;
and the "receiver":
serverSocket = new ServerSocket(SERVERPORT);
// listen for incoming clients
Socket client = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()),8*1024);
This works great! But I want to send a file from one device to another instead of an int. How can I make this?????
There's an open source project released by Google you can take a look at it and have a general idea about connecting devices and sharing files between them.
Here is the link: android-fileshare
You need to package the data to the stream through a some kind of data format. One way to do this is to use the common MIME data format which is commonly used to send attachment in email.
I have answered other question related to sending binary via socket using this format in the following SO Question - android add filename to bytestream. You could check the accepted answer for that question.
For your reference, I just copied the code for sending and receiving through socket from that question below.
And the code to read it back below using MimeBodyPart that is part of JavaMail.