0

I am working on an android app which let's user record a video 30sec long and then I need to save this video file on the server. I am using a web service which accepts the video file in the byteArray format. So, I don't know how to convert and compress .mp4 file and then send it to web service over the wire. Please help...

Sheetal Jadhwani
  • 75
  • 1
  • 3
  • 9

1 Answers1

1

try

HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;


    String pathOfYourFile = "/sdcard/videoName.3gp";
    String urlServer = "http://....../uploadvideo.php";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
    FileInputStream fileInputStream = new FileInputStream(new File(pathOfYourFile) );

    URL url = new URL(urlServer);
    connection = (HttpURLConnection) url.openConnection();

    // Allow Inputs & Outputs
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    // Enable POST method
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary");

    outputStream = new DataOutputStream( connection.getOutputStream() );
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathOfYourFile );
    outputStream.writeBytes(lineEnd);

    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];

    // Read file
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0)
    {
    outputStream.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }

    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // Responses from the server (code and message)
    int serverResponseCode = connection.getResponseCode();
     String serverResponseMessage = connection.getResponseMessage();
     Log.d("ServerCode",""+serverResponseCode);
     Log.d("serverResponseMessage",""+serverResponseMessage);
    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
}
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
  • Send file on server: PHP script + Android code https://reecon.wordpress.com/2010/04/25/uploading-files-to-http-server-using-post-android-sdk/ – gc986 Jun 24 '15 at 11:03