Android開發之合并檔案的幾種方式

來源:互聯網
上載者:User

標籤:android   style   color   io   os   ar   java   for   sp   

        以下介紹合并檔案的幾種方式,並通過合并amr檔案來舉例介紹合并檔案的詳細流程。amr格式的檔案頭是6位元組,所以在進行檔案合并的時候要減去除第一個檔案以外的其它檔案的檔案頭。

注意:不同檔案的檔案頭是不一樣的,所以在合并的時候依據不同檔案對應的減去合并檔案的檔案頭。

步驟一:擷取要合并的檔案及建立合并後儲存的檔案

/**用於存放要合并的檔案的集合**/List<File>tempFiles=new ArrayList<File>();/**合并之後的檔案**/File finalFile;
/** * 建立用於合并之後的檔案 * @param isTempFile 是否為暫時檔案 * @return soundFile File * */private File getFile(boolean isTempFile) {// TODO Auto-generated method stubfinalFile=null;if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {Log.w("Waring", "檢測到你的手機沒有插入SD卡,請插入SD後再試!");}//擷取系統的24小時制時間作為檔案名稱(HH為24小時制,hh為12小時制)SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss",Locale.getDefault());String fileName=simpleDateFormat.format(new Date())+".amr";if (isTempFile) {//假設是暫時檔案fileName="temp"+fileName;}try {File parentFile= new File(Environment.getExternalStorageDirectory().getCanonicalFile()+"/"+"Recorder");if (!parentFile.exists()||parentFile==null) {//假設檔案夾不存在parentFile.mkdirs();//建立parentFile檔案夾}finalFile=new File(parentFile, fileName);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return finalFile;}

步驟二:合并檔案

方式一: 通過FileOutputStream、與FileInputStream方式

/** * 通過FileOutputStream、與FileInputStream方式 * 將多個檔案進行合并,並刪除原檔案 * */public void mergeFiles1() {// TODO Auto-generated method stubif (tempFiles.isEmpty()) return;//假設還沒錄製則,不進行合并File realFile=getFile(false);try {FileOutputStream fos=new FileOutputStream(realFile);for (int i = 0; i < tempFiles.size(); i++) {//遍曆tempFiles集合,合并全部暫時檔案FileInputStream fis=new FileInputStream(tempFiles.get(i));byte[] tmpBytes = new byte[fis.available()];int length = tmpBytes.length;//檔案長度//標頭檔if(i==0){while(fis.read(tmpBytes)!=-1){fos.write(tmpBytes,0,length);}}//之後的檔案,去迴轉檔案就能夠了.amr格式的檔案的頭資訊為 6位元組else{while(fis.read(tmpBytes)!=-1){fos.write(tmpBytes,6,length-6);}}fos.flush();fis.close();}fos.close();//全部的檔案合并結束,關閉輸出資料流Log.i("info", "此次錄音檔案:"+realFile.getName()+" 已儲存到:"+realFile.getAbsolutePath()+"檔案夾下");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}//刪除合并過的暫時檔案for (File file:tempFiles) {if (file.exists()) {file.delete();}}}

方式二: 通過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();        //以下應該依據不同檔案減去對應的檔案頭(這裡沒有剪去檔案頭,實際應用中應當減去)        inFileChannel.transferTo(0, inFileChannel.size(), mFileChannel);                     inFileChannel.close();    }           fos.close();    mFileChannel.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}

方式三:通過RandomAccessFile方式

/** * 通過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);//跳過amr檔案的檔案頭    }    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開發之合并檔案的幾種方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.