Android development: several methods of merging files, android

Source: Internet
Author: User

Android development: several methods of merging files, android

The following describes how to merge files, and introduces the specific process of merging files by merging amr files. The file header in amr format is 6 bytes. Therefore, when merging files, remove the file headers of files other than the first file.

Note: The file headers of different files are different. Therefore, when merging files, subtract the file headers of the merged files from the corresponding files.

Step 1: Obtain the file to be merged and the file to be saved after creation and merging

/** Used to store the set of files to be merged **/List <File> tempFiles = new ArrayList <File> (); /** merged files **/File finalFile;
/*** Create a File for merging * @ param isTempFile: whether it is a temporary File * @ return soundFile File **/private File getFile (boolean isTempFile) {// TODO Auto-generated method stubfinalFile = null; if (! Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {Log. w ("Waring", "No SD card is inserted to your mobile phone. Please insert SD and try again! ");} // Obtain the system's 24-hour time as the file name (HH: 24-hour, hh: 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 the parentFile directory} finalFile = new File (parentFile, fileName);} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return finalFile ;}

Step 2: Merge files

Method 1: FileOutputStream and FileInputStream

/*** Use FileOutputStream and FileInputStream to merge multiple files and delete the original file **/public void mergeFiles1 () {// TODO Auto-generated method stubif (tempFiles. isEmpty () return; // if no recording is available, Do not merge File realFile = getFile (false); try {FileOutputStream fos = new FileOutputStream (realFile ); for (int I = 0; I <tempFiles. size (); I ++) {// traverses the tempFiles set and merges all temporary files FileInputStream fiis = new FileInputStream (tempFiles. get (I); byte [] tmpByte S = new byte [FCM. available ()]; int length = tmpBytes. length; // file length // header file if (I = 0) {while (FS. read (tmpBytes )! =-1) {fos. write (tmpBytes, 0, length) ;}// after the file, remove the header file. the header information of an amr file is 6 bytes else {while (FS. read (tmpBytes )! =-1) {fos. write (tmpBytes, 6, length-6);} fos. flush (); FCM. close ();} fos. close (); // all files are merged to close the output stream Log. I ("info", "this recording file:" + realFile. getName () + "saved to:" + realFile. getAbsolutePath () + "directory");} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();} // Delete the merged temporary File for (file: tempFiles) {if (File. exists () {file. delete ();}}}

Method 2: FileChannel

/*** FileChannel **/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 subtract the corresponding file header from different files (the file header is not cut here and should be subtracted from the actual application) inFileChannel. transferTo (0, inFileChannel. size (), mFileChannel); inFileChannel. close ();} fos. close (); mFileChannel. close ();} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace ();}}

Method 3: RandomAccessFile

/*** Use RandomAccessFile **/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 amr File Header} 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 ();}}



[Android Development]: several data transmission methods between activities

1. intent is used to transmit data Intent tables. In many cases, Android Intent is used to transmit data between various activities. This is also an official data transfer method for Android. Requirement 1: jump from an Activity (IntentDemo) to another Activity (Other). The Demo of the Data Program passed by Intent is as follows: IntentDemo. javapackage com. android. intentdemo; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. widget. button; public class IntentDemo extends Activity {private Button button; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); initComponent (); button. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (IntentDemo. this, Other. class); // transmits data Intent in intent. putExtra ("name", "AHuier"); intent. putExtra ("age", 22); intent. putExtra ("address", "XiaMen"); // start Intent startActivity (intent) ;}};} private void initComponent () {button = (Button) findViewById (R. id. button) ;}} other. javapackage com. android. intentdemo; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. widget. textView; public class Other extends Activity {private TextView textView; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. other); initComponent (); Intent intent = getIntent (); int age = intent. getIntExtra (...... remaining full text>

How many storage methods are persistent in android?

There are four types, which will exist after Shutdown:

File, SharedPreference, SQLite, and ContentProvider

Each has its own advantages and disadvantages.

File is mainly used to store large files, but you need to use space in your sdcard, such as saving a binary file. the operation method is similar to that of java, that is, to open a FileInputStream/FileOutPutStream, convert it to InputStream/outPutStream, and then read/write bytes.

SharedPrefreence is mainly used to store simple data types. it is not suitable for storing files. For example, after the First Login of QQ, the account and password can be saved (the user clicks and remembers the password). Then, when the user logs on again, the user enters directly without entering the password.

SQLite is a small database mainly used to store record tables, such as the ranking of multiple players' points. a n-Row Table consisting of fields such as id, score, and level is required.

ContentProvider is also known as the content provider. It provides A way to implement communication between two unrelated applications. For example, if program A saves the next data in the specified ContentProvider, program B can obtain the data.

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.