//http://lakjeewa.blogspot.fr/2012/03/simple-android-application-to-send-file.html


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SimpleClientActivity extends Activity 
{

    private Socket client;
    private FileInputStream fileInputStream;
    private BufferedInputStream bufferedInputStream;
    private OutputStream outputStream;
    private Button button;
    private TextView text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.button1);   //reference to the send button
        text = (TextView) findViewById(R.id.textView1);   //reference to the text view

        //Button press event listener
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                File file = new File("/mnt/sdcard/input.jpg"); //create file instance

                try {

                    client = new Socket("10.0.2.2", 8081);

                    byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file

                    fileInputStream = new FileInputStream(file);
                    bufferedInputStream = new BufferedInputStream(fileInputStream);

                    bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file

                    outputStream = client.getOutputStream();

                    outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
                    outputStream.flush();
                    bufferedInputStream.close();
                    outputStream.close();
                    client.close();

                    text.setText("File Sent");


                } catch (UnknownHostException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}


<manifest android:versioncode="1" android:versionname="1.0" package="lakj.comspace.simpleclient"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-sdk android:minsdkversion="10">
        <uses-permission android:name="android.permission.INTERNET"></uses-permission>

        <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
            <activity android:label="@string/app_name" android:name=".SimpleClientActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN">

                        <category android:name="android.intent.category.LAUNCHER">
                        </category>
                    </action>
                </intent-filter>
            </activity>
        </application>

    </uses-sdk>
</manifest>


<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical"
              xmlns:android="http://schemas.android.com/apk/res/android">

    <textview android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content"
              android:textappearance="?android:attr/textAppearanceLarge">

        <button android:id="@+id/button1" android:layout_gravity="center" android:layout_height="wrap_content"
                android:layout_width="wrap_content" android:text="Send">
        </button>

    </textview>
    
</linearlayout>



/*
 * This is a simple server application
 * This server receive a file from a Android client and save it in a given place.
 * Author by Lak J Comspace
 */
package simpleserver;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

    private static ServerSocket serverSocket;
    private static Socket clientSocket;
    private static InputStream inputStream;
    private static FileOutputStream fileOutputStream;
    private static BufferedOutputStream bufferedOutputStream;
    private static int filesize = 10000000; // filesize temporary hardcoded 
    private static int bytesRead;
    private static int current = 0;

    public static void main(String[] args) throws IOException {


        serverSocket = new ServerSocket(8081);  //Server socket

        System.out.println("Server started. Listening to the port 8081");


        clientSocket = serverSocket.accept();


        byte[] mybytearray = new byte[filesize];    //create byte array to buffer the file

        inputStream = clientSocket.getInputStream();
        fileOutputStream = new FileOutputStream("/home/pi/JAVA/patoche.txt");                               //("D:\\output.jpg");
        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

        System.out.println("Receiving...");

        //following lines read the input slide file byte by byte
        bytesRead = inputStream.read(mybytearray, 0, mybytearray.length);
        current = bytesRead;

        do {
            bytesRead = inputStream.read(mybytearray, current, (mybytearray.length - current));
            if (bytesRead >= 0) {
                current += bytesRead;
            }
        } while (bytesRead > -1);


        bufferedOutputStream.write(mybytearray, 0, current);
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
        inputStream.close();
        clientSocket.close();
        serverSocket.close();

        System.out.println("Sever recieved the file");

    }
}
