Upload large Video File on Server
When we large or small file uploading on server then we have need to Convert into bytearray byte[] and My Researched base you can not convert file in to bytearray byte[] of over 10Mb easily.
In that Case you can easily get bytearray without any Memory error from this way 10 to 60 Mb file convert.
You can get byte[] data from file then follow this way:
First Way:
String filepath = "/mnt/sdcard/video.mp4";
String chaunksize = "100000";
byte[] inputDatavideo = readbytedata(filepath, chaunksize );
public byte[] readbytedata(String SourceFileName, int CHUNK_SIZE)
throws IOException {
File willBeRead = new File(SourceFileName);
int FILE_SIZE = (int) willBeRead.length();
ArrayList<String> nameList = new ArrayList<String>();
int NUMBER_OF_CHUNKS = 0;
byte[] file_data = new byte[FILE_SIZE];
try {
InputStream inStream = null;
int totalBytesRead = 0;
try {
inStream = new BufferedInputStream(new FileInputStream(
willBeRead));
// int bytesRead = inStream.read(file_data, 0, FILE_SIZE);
while (totalBytesRead < FILE_SIZE) {
String PART_NAME = "data" + NUMBER_OF_CHUNKS + ".bin";
int bytesRemaining = FILE_SIZE - totalBytesRead;
if (bytesRemaining < CHUNK_SIZE)
// Remaining Data Part is
// Smaller Than // CHUNK_SIZE // CHUNK_SIZE is assigned to
// remainvolume
{
CHUNK_SIZE = bytesRemaining;
System.out.println("CHUNK_SIZE: " + CHUNK_SIZE);
}
// Temporary Byte Array
int bytesRead = inStream.read(file_data, totalBytesRead,
CHUNK_SIZE);
if (bytesRead > 0) // If bytes read is not empty
{
totalBytesRead += bytesRead;
NUMBER_OF_CHUNKS++;
}
}
} finally {
inStream.close();
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return file_data;
}
Second Way:
ArrayList<byte[]> outputvideofile = bytearraydata.readbytedatass(filepath, chaunksize );
public ArrayList<byte[]> readbytedatass(String SourceFileName,
int CHUNK_SIZE) throws IOException {
// log.i("File Is Reading "+ SourceFileName );
File willBeRead = new File(SourceFileName);
int FILE_SIZE = (int) willBeRead.length();
ArrayList<String> nameList = new ArrayList<String>();
ArrayList<byte[]> byteList = new ArrayList<byte[]>();
int NUMBER_OF_CHUNKS = 0;
byte[] file_data = new byte[FILE_SIZE];
try {
InputStream inStream = null;
int totalBytesRead = 0;
try {
inStream = new BufferedInputStream(new FileInputStream(
willBeRead));
while (totalBytesRead < FILE_SIZE) {
String PART_NAME = "data" + NUMBER_OF_CHUNKS + ".bin";
int bytesRemaining = FILE_SIZE - totalBytesRead;
if (bytesRemaining < CHUNK_SIZE) // Remaining Data Part is
// Smaller Than
// CHUNK_SIZE
// CHUNK_SIZE is assigned to remain volume
{
CHUNK_SIZE = bytesRemaining;
System.out.println("CHUNK_SIZE: " + CHUNK_SIZE);
}
// temporary = new byte[CHUNK_SIZE]; // Temporary Byte Array
int bytesRead = inStream.read(file_data, totalBytesRead,
CHUNK_SIZE);
if (bytesRead > 0) // If bytes read is not empty
{
totalBytesRead += bytesRead;
NUMBER_OF_CHUNKS++;
}
nameList.add("/sdcard/" + PART_NAME);
byteList.add(file_data);
}
} finally {
inStream.close();
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return byteList;
}