Three ways to merge files with Android code

Source: Internet
Author: User

The file header in AMR format is 6 bytes, and the file header is subtracted from files other than the first file when the file is merged. Here are a few ways to merge files, and combine AMR files to illustrate the process of merging files.

Note: The file header of different files is not the same, so when merging, the file header of the merged file is subtracted according to the different files. Specifically you can learn the Android Development Tutorials .

Step one: Get the files to be merged and create the files saved after merging

/** is used to hold the collection of files to be merged **/
List<file>tempfiles=new arraylist<file> ();
/** files after merging **/
File Finalfile;

/**
* Creates a file for merging
* @param istempfile is a temporary file
* @return soundfile file
* */
Private file GetFile ( Boolean istempfile) {
//TODO auto-generated method Stub
Finalfile=null;
if (! Environment.getexternalstoragestate ().
Equals (environment.media_mounted)) {
LOG.W ("Waring", "detected that your phone is not plugged into an SD card, please insert SD and try again!") ”);
}
//Get the system's 24-hour time as file name (HH is 24-hour, HH is 12-hour)
SimpleDateFormat simpledateformat=new SimpleDateFormat (
" Yyyy-mm-dd-hh-mm-ss ", Locale.getdefault ());
String Filename=simpledateformat.format (New Date ()) + ". Amr";
if (istempfile) {//If it is a temporary file
filename= "temp" +filename;
}
try {
File parentfile= new file (Environment.getexternalstoragedirectory ()
. Getcanonicalfile () + "/" + " Recorder ");
if (!parentfile.exists () | | | Parentfile==null) {//If the directory does not exist
Parentfile.mkdirs ();//Create parentfile directory
}

Finalfile=new File (Parentfile, fileName);
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
return finalfile;

Step Two: Merge files

Way one: Through FileOutputStream, and FileInputStream Way

/**
* Merge multiple files with FileOutputStream, FileInputStream mode
*, and delete the original file
* */
public void MergeFiles1 () {
// TODO auto-generated Method Stub
if (Tempfiles.isempty ()) return;//do not merge
If you have not recorded it realfile=getfile (false);
try {
FileOutputStream fos=new fileoutputstream (realfile);
for (int i = 0; i < tempfiles.size (); i++) {//Traverse tempfiles Collection, merge all temporary files
FileInputStream fis=new fileinputstream (TEMPFI Les.get (i));
byte[] tmpbytes = new byte[fis.available ()];
int length = tmpbytes.length;//file length
//header file
if (i==0) {
while (Fis.read (tmpbytes)!=-1) {
Fos.write ( Tmpbytes,0,length);
}
}
//After the file, remove the header file. The header information for the AMR format file is 6 bytes
else{
while (Fis.read (tmpbytes)!=-1) {
Fos.write ( TMPBYTES,6,LENGTH-6);

}
Fos.flush ();
Fis.close ();
}
Fos.close ();//All Files merge end, close output stream
LOG.I ("info", "This recording file:" +realfile.getname () + "saved to:" +
Realfile.getabsolutepath () + "directory");
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Delete a merged temporary file
for (File file:tempfiles) {
if (file.exists ()) {
File.delete ();
}
}
}

Way two: Through FileChannel way

/**
* by FileChannel Way
* */
public void MergeFiles2 () {
File Realfile=getfile (FALSE);
FileChannel Mfilechannel;
try {
FileOutputStream fos=new FileOutputStream (realfile);
Mfilechannel=fos.getchannel ();
FileChannel Infilechannel;
for (File file:tempfiles) {
Infilechannel=new fileinputstream (file). Getchannel ();

The following should be based on the different files minus the corresponding file header (here does not cut the file header, the actual application should be subtracted)
Infilechannel.transferto (0, Infilechannel.size (), Mfilechannel);
Infilechannel.close ();
}
Fos.close ();
Mfilechannel.close ();
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

Way three: Through Randomaccessfile way

/**
* by Randomaccessfile Way
* */
public void MergeFiles3 () {
try{
File Realfile=getfile (FALSE);
FileOutputStream fos = new FileOutputStream (realfile);
Randomaccessfile RA = null;
for (int i = 0; i < tempfiles.size (); i++) {
RA = new Randomaccessfile (Tempfiles.get (i), "R");
if (i! = 0) {
Ra.seek (6);//Skip the file header of the AMR file
}
byte[] buffer = new byte[1024 * 8];
int len = 0;
while (len = ra.read (buffer))! =-1) {
Fos.write (buffer, 0, Len);
}

}
Ra.close ();
Fos.close ();
} catch (Exception e) {
E.printstacktrace ();
}
}

There are more Mobile Internet tutorial information, etc. please pay attention to e Mentor Network.

Three ways to merge files with Android code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.