Skip to content Skip to sidebar Skip to footer

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

Solution 1:

To split file into parts (chunks):

publicstatic List<File> splitFile(File f)throws IOException {
    intpartCounter=1;
    List<File> result = newArrayList<>();
    intsizeOfFiles=1024 * 1024;// 1MBbyte[] buffer = newbyte[sizeOfFiles]; // create a buffer of bytes sized as the one chunk sizeBufferedInputStreambis=newBufferedInputStream(newFileInputStream(f));
    Stringname= f.getName();

    inttmp=0;
    while ((tmp = bis.read(buffer)) > 0) {
        FilenewFile=newFile(f.getParent(), name + "." + String.format("%03d", partCounter++)); // naming files as <inputFileName>.001, <inputFileName>.002, ...FileOutputStreamout=newFileOutputStream(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:

publicstaticvoidmergeFiles(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

Post a Comment for "How To Upload Big Videos Files To Server Quickly In Android"