title: Android downloading video from server
body: <p>i want to dowload video using phone from my local server and save it on sdcard, but android client crashes, i need a little help. I used this code <a href="http://www.rgagnon.com/javadetails/java-0542.html" rel="nofollow">http://www.rgagnon.com/javadetails/java-0542.html</a></p>
  
  <p>Here is server code:</p>
  
  <pre><code>import java.io.BufferedInputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class Server {
  
    public final static int SOCKET_PORT = 13267;  // you may change this
    public final static String FILE_TO_SEND = "d:/sintel.mp4";  // you may change this
  
    public static void main (String [] args ) throws IOException {
      FileInputStream fis = null;
      BufferedInputStream bis = null;
      OutputStream os = null;
      ServerSocket servsock = null;
      Socket sock = null;
      try {
        servsock = new ServerSocket(SOCKET_PORT);
        while (true) {
          System.out.println("Waiting...");
          try {
            sock = servsock.accept();
            System.out.println("Accepted connection : " + sock);
            // send file
            File myFile = new File (FILE_TO_SEND);
            byte [] mybytearray  = new byte [(int)myFile.length()];
            fis = new FileInputStream(myFile);
            bis = new BufferedInputStream(fis);
            bis.read(mybytearray,0,mybytearray.length);
            os = sock.getOutputStream();
            System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)");
            os.write(mybytearray,0,mybytearray.length);
            os.flush();
            System.out.println("Done.");
          }
          finally {
            if (bis != null) bis.close();
            if (os != null) os.close();
            if (sock!=null) sock.close();
          }
        }
      }
      finally {
        if (servsock != null) servsock.close();
      }
    }
  }
  </code></pre>
  
  <p>Client code:</p>
  
  <pre><code>import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  
  
  import java.io.BufferedOutputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.net.Socket;
  
  
  
  
  public class MainActivity extends ActionBarActivity {
  
      public final static int SOCKET_PORT = 13267;      // you may change this
      public final static String SERVER = "127.0.0.1";  // localhost
      public final static String FILE_TO_RECEIVED = "/storage/external_SD/sintel.mp4";;  // you may change this, I give a
      // different name because i don't want to
      // overwrite the one used by server...
  
      public final static int FILE_SIZE = 14000000; // file size temporary hard coded
      // should bigger than the file to be downloaded
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
  
  
  
      }
  
  
  
      public void buttonClicked(View view)throws IOException{
  
          int bytesRead;
          int current = 0;
          FileOutputStream fos = null;
          BufferedOutputStream bos = null;
          Socket sock = null;
          try {
              sock = new Socket(SERVER, SOCKET_PORT);
              System.out.println("Connecting...");
  
              // receive file
              byte [] mybytearray  = new byte [FILE_SIZE];
              InputStream is = sock.getInputStream();
              fos = new FileOutputStream(FILE_TO_RECEIVED);
              bos = new BufferedOutputStream(fos);
              bytesRead = is.read(mybytearray,0,mybytearray.length);
              current = bytesRead;
  
              do {
                  bytesRead =
                          is.read(mybytearray, current, (mybytearray.length-current));
                  if(bytesRead &gt;= 0) current += bytesRead;
              } while(bytesRead &gt; -1);
  
              bos.write(mybytearray, 0 , current);
              bos.flush();
              // System.out.println("File " + FILE_TO_RECEIVED
              //       + " downloaded (" + current + " bytes read)");
          }
          finally {
              if (fos != null) fos.close();
              if (bos != null) bos.close();
              if (sock != null) sock.close();
          }
  
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>xml:</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="download video"
          android:id="@+id/bdownload"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="199dp"
          android:onClick="buttonClicked"
          android:nestedScrollingEnabled="false" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>and manifest:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.aidan.client" &gt;
  
      &lt;uses-permission android:name="android.permission.INTERNET" &gt;
      &lt;/uses-permission&gt;
      &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/&gt;
      &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>