How to Upload Large Video File to Server in Android?

Uploading large video files on the android server in the earlier time was more complex compared to the present period. In the past, users could upload files of less than 8MB. But in this internet era, it has become a maximum of 15MB. But what about the large files?

 

Well, in the new process, you can transform the file into a small one and later can upload it. Here, we will cover an easy guideline on how to upload large video file to server in android. 

 

Follow the instructions step-by-step, and set your mind because the process will take many steps with essential codes. Are you ready?

Steps on How to Upload Large Video File to Server in Android

 

You have to chunk the video file at first and then have to upload it. There will be no quality drop, the system will upload the video exactly at the same quality you want.

 

We will discuss how you can upload a large file on the android server using OKHTTP.

 

Download and install the OKHTTP third-party application from the authentic source and start the following process:

 

  • Open the application and put the cursor on the Pick File logo to enable a new dialogue box to select the large video file and after the process, you can upload the large video.

 

 

 

how to upload large video file to server in android

 

 

 

  • Now click on the Main. Activity Java from the above screen.

 

 

 

how to upload large video file to server in android

 

 

 

  • From the left screen, click on the Manifests option.

 

 

 

how to upload large video file to server in android

 

 

 

  • On the manifest option, you will see the given internet code, which is the internet access. Enable the permission option for external function.

 

 

 

how to upload large video file to server in android

 

 

 

  • Select the build.gradle app file, it will open up the code box where you have to make a few changes to turn on the chunked streaming mode through coding. The mode will convert the video into a small size and will upload it, maintaining the quality. Now Ok the Http library.

 

 

 

how to upload large video file to server in android

 

 

 

  • The system will use as the network library, along with the nbsp library. The library is also called the material library, and you can use it to pick a file.

 

 

 

how to upload large video file to server in android

 

 

 

  • In this section, you can also create your own code, but technically, it may generate a long video.

 

  • Now go back to the main activity java section and type or paste the given

 

public String uploadFileToServer(String filename, String targetUrl) {

String response = “error”;

Log.e(“Image filename”, filename);

Log.e(“url”, targetUrl);

HttpURLConnection connection = null;

DataOutputStream outputStream = null;

// DataInputStream inputStream = null;

 

String pathToOurFile = filename;

String urlServer = targetUrl;

String lineEnd = “\r\n”;

String twoHyphens = “–“;

String boundary = “*****”;

DateFormat df = new SimpleDateFormat(“yyyy_MM_dd_HH:mm:ss”);

 

int bytesRead, bytesAvailable, bufferSize;

byte[] buffer;

int maxBufferSize = 1 * 1024;

try {

FileInputStream fileInputStream = new FileInputStream(new File(

pathToOurFile));

 

URL url = new URL(urlServer);

connection = (HttpURLConnection) url.openConnection();

 

// Allow Inputs & Outputs

connection.setDoInput(true);

connection.setDoOutput(true);

connection.setUseCaches(false);

connection.setChunkedStreamingMode(1024);

// Enable POST method

connection.setRequestMethod(“POST”);

 

connection.setRequestProperty(“Connection”, “Keep-Alive”);

connection.setRequestProperty(“Content-Type”,

“multipart/form-data;boundary=” + boundary);

 

outputStream = new DataOutputStream(connection.getOutputStream());

outputStream.writeBytes(twoHyphens + boundary + lineEnd);

 

String connstr = null;

connstr = “Content-Disposition: form-data; name=\”uploadedfile\”;filename=\””

+ pathToOurFile + “\”” + lineEnd;

Log.i(“Connstr”, connstr);

 

outputStream.writeBytes(connstr);

outputStream.writeBytes(lineEnd);

 

bytesAvailable = fileInputStream.available();

bufferSize = Math.min(bytesAvailable, maxBufferSize);

buffer = new byte[bufferSize];

 

// Read file

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

Log.e(“Image length”, bytesAvailable + “”);

try {

while (bytesRead > 0) {

try {

outputStream.write(buffer, 0, bufferSize);

} catch (OutOfMemoryError e) {

e.printStackTrace();

response = “outofmemoryerror”;

return response;

}

bytesAvailable = fileInputStream.available();

bufferSize = Math.min(bytesAvailable, maxBufferSize);

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

}

} catch (Exception e) {

e.printStackTrace();

response = “error”;

return response;

}

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.i(“Server Response Code “, “” + serverResponseCode);

Log.i(“Server Response Message”, serverResponseMessage);

 

if (serverResponseCode == 200) {

response = “true”;

}

 

String CDate = null;

Date serverTime = new Date(connection.getDate());

try {

CDate = df.format(serverTime);

} catch (Exception e) {

e.printStackTrace();

Log.e(“Date Exception”, e.getMessage() + ” Parse Exception”);

}

Log.i(“Server Response Time”, CDate + “”);

 

filename = CDate

+ filename.substring(filename.lastIndexOf(“.”),

filename.length());

Log.i(“File Name in Server : “, filename);

 

fileInputStream.close();

outputStream.flush();

outputStream.close();

outputStream = null;

} catch (Exception ex) {

// Exception handling

response = “error”;

Log.e(“Send file Exception”, ex.getMessage() + “”);

ex.printStackTrace();

}

return response;

}

 

  • Now select the file size, _max_filesize=200MB.

 

 

 

how to upload large video file to server in android

 

 

 

  • A new android window will appear, click on allow to insert the video file.

 

 

 

how to upload large video file to server in android

 

 

 

  • Select the file and wait for a few seconds to a minute.

 

 

 

how to upload large video file to server in android

 

 

Frequently Asked Questions (FAQs):

 

 

 

  • Which Method is the Best to Send Video Files on Another Android?

 

Actually, there are multiple applications available through which you can transfer or send video files to another android device, but most of them cannot keep the quality the same. In this case, if you want to keep the quality same and full HD you can use Google drive.

 

  • What is the Right Way to Send Large Video on Android?

 

Drive to the google drive, tick on the new+ and browse the file from the device, and press upload. The system will upload the file on the drive after full download, click on the share option, edit the privacy, copy the link and send it to the person.

 

  • Why Am I Unable to Upload My Large Video on Android Server?

 

Because of the new system policy, if you want to upload a large video, you have to use the chunking method, this system will shrink the video into a smaller version.

 

Conclusion

 

Although the discussed procedures are lengthy and bothersome, we have deliberated the easiest and correct method. In this process, you can change the video limit as much as possible. The main activity log will accept and apply only instructed code, not less and more.

 

That is why we have discoursed this specific application using the guideline for how to upload large video file to server in android. By using the guideline, we hope you will be able to upload your file to the android server.

 

 

You may also interested to know:

 

 

Leave a Reply

Your email address will not be published.