Krish
Krish

Reputation: 4232

How to upload big videos files to server quickly in android

Hi I am uploading Large video files to server using Volley Multi-part Api but it takes much time for upload to server

Is it better to split my video files and send to server? If it is better please provide me code how can I do that, If not what is the best way to uploading big videos files to server quickly?

Upvotes: 2

Views: 2558

Answers (1)

Vladyslav Matviienko
Vladyslav Matviienko

Reputation: 10881

To split file into parts (chunks):

public static List<File> splitFile(File f) throws IOException {
    int partCounter = 1;
    List<File> result = new ArrayList<>();
    int sizeOfFiles = 1024 * 1024;// 1MB
    byte[] buffer = new byte[sizeOfFiles]; // create a buffer of bytes sized as the one chunk size

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
    String name = f.getName();

    int tmp = 0;
    while ((tmp = bis.read(buffer)) > 0) {
        File newFile = new File(f.getParent(), name + "." + String.format("%03d", partCounter++)); // naming files as <inputFileName>.001, <inputFileName>.002, ...
        FileOutputStream out = new FileOutputStream(newFile);
        out.write(buffer, 0, tmp);//tmp is chunk size. Need it for the last chunk, which could be less then 1 mb.
        result.add(newFile);
    }
    return result;
}

This method will split your file to chunks of size of 1MB (excluding the last chunk). After words you can send all these chunks too the server.

Also if you need to merge these files:

public static void mergeFiles(List<File> files, File into)
        throws IOException {
   BufferedOutputStream mergingStream = new BufferedOutputStream(new FileOutputStream(into))
    for (File f : files) {
        InputStream is = new FileInputStream(f);
        Files.copy(is, mergingStream);
        is.close();
    }
    mergingStream.close();
}

Just in case if your server is in Java also

Upvotes: 5

Related Questions