In my app i implement a download manager that using multi connection for download a file and save content in temp files and finally when download completed merge these files in a single file.
My problem is the last step (merging files) take too long to be completed even more than downloading the file(for example a file with 250 Kb downloaded in 5 second and merged in about 10 - 15 second).
Im using this method for merging temp file :
private void mergeAndActivate() {
new AsyncTask<Void, Integer, Void>() {
@Override
protected void onPreExecute() {
// only toast a message
}
@Override
protected Void doInBackground(Void... params) {
long leninfile=0, leng=0;
int count=0, data=0;
try {
String filePath =download.getFile().getPath();
File filename = new File(filePath);
RandomAccessFile outfile = new RandomAccessFile(filename,"rw");
while(true) {
filename = getTempFile(count);
if (filename.exists()) {
RandomAccessFile infile = new RandomAccessFile(filename,"r");
data=infile.read();
while(data != -1) {
outfile.write(data);
data=infile.read();
}
leng++;
infile.close();
count++;
} else break;
}
outfile.close();
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
deleteTempFiles();
// Toast a mesage
}
}.execute();
}