Android開發之合併檔案的幾種方式
下面介紹合併檔案的幾種方式,並通過合併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 stub finalFile=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 block e.printStackTrace(); } return finalFile; }
步驟二:合併檔案
方式一: 通過FileOutputStream、與FileInputStream方式
/** * 通過FileOutputStream、與FileInputStream方式 * 將多個檔案進行合併,並刪除原檔案 * */ public void mergeFiles1() { // TODO Auto-generated method stub if (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 block e.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 block e.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開發之合併檔案的幾種方式
下面介紹合併檔案的幾種方式,並通過合併amr檔案來舉例介紹合併檔案的具體流程。amr格式的檔案頭是6位元組,所以在進行檔案合併的時候要減去除第一個檔案以外的其他檔案的檔案頭。 注意:不
Android開發 之 view的幾種佈局方式及實踐
引言 通過前面兩篇: 我們對Android應用程式執行原理及佈局檔案可謂有了比較深刻的認識和理解,並且用“Hello World!”程式來實踐證明了。在繼續深入Android開發之旅之前,有必要解決前兩篇中沒有介紹的遺留問題:View的幾種佈局顯示方法,以後就不會在針對佈局方面做過多的介紹。View的佈局顯
android開發之字串常用幾種操作
public String substring(int beginIndex);返回一個新的字串,它是此字串的一個子字串。該子字串始於指定索引處的字元,一直到此字串末尾 public String substring(int beginIndex,int endIndex)
Android載入圖片資源的幾種方式
1. 圖片放在sdcard中, Bitmap imageBitmap = BitmapFactory.decodeFile(path) (path 是圖片的路徑,跟目錄是/sdcard) 2. 圖片在專案的res資料夾下面 //得到applicat
Android 程式碼設定Color的幾種方式
系統自帶的顏色類 tx.setTextColor(android.graphics.Color.RED); 利用數字設定 tx.setTextColor(0xffff00f); 利用xml中已經定義好的顏色程式碼 tx.setTextColor(getResources(
Android程序間通訊的幾種方式
定義多程序 Android應用中使用多程序只有一個辦法(用NDK的fork來做除外),就是在AndroidManifest.xml中宣告元件時,用android:process屬性來指定。 不知定process屬性,則預設執行在主程序中,主程序名字為包名。 andr
Android 非同步更新UI的幾種方式
一、為什麼不能在主執行緒更新UI ViewRootImpl通過 checkThread() 方法檢查更新UI操作是否是在主執行緒當中 原因:Android的UI是執行緒不安全的,存在併發訪問的問
Android: WebView載入網頁的幾種方式及網路異常處理
1.載入本地assert目錄下檔案(error.html) webcontent.loadUrl(" file:///android_asset/error.html "); 2.載入網路url(http://www.csdn.com) webcontent.loadUrl(" http://www.csd
android延時執行的幾種方式
在專案中有很多的方法可能我們不需要立即執行,那麼就需要延時。 首先第一種就是執行緒: sleep會阻塞執行緒 new Thread (new Runnable(){ public void run(){ Thread.sleep(time
Asp.net下載檔案幾種方式
protected void Button1_Click(object sender, EventArgs e) { /* 微軟為Response物件提供了一個新的方法TransmitFile來解決使用Response.BinaryWrite 下載超過400mb的檔案時導致Aspnet_wp.
android Button 監聽的幾種方式
android button控制元件目前主要有如下幾種監聽方式: 一個button控制元件對應一個監聽: Button buttontest; buttontest = (Button) findViewById(R.id.button1); butto
【Android】獲取Bitmap的幾種方式總結
ApplicationInfo appInfo = getApplicationInfo(); //得到該圖片的id(name 是該圖片的名字,"drawable" 是該圖片存放的目錄,appInfo.packageName是應用程式的包) int resID = getResources().getIde
android自定義view的幾種方式
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_view_item, null); XXXX = (ItemView)convertView; 假如我想自定義一個listview,我之前的
IOS 之資料庫 的幾種方式
在iOS開發過程中,不管是做什麼應用,都會碰到資料儲存的問題。將資料儲存到本地,能夠讓程式的執行更加 流暢,不會出現讓人厭惡的菊花形狀,使得使用者體驗更好。下面介紹⼀一下資料儲存的方式: 1.NSKeyedArchiver:採用歸檔的形式來儲存資料,該資料物件需要遵守NSCoding協議,並且該物件對應
asp.net 下載檔案幾種方式
protected void Button1_Click(object sender, EventArgs e) { /* 微軟為Response物件提供了一個新的方法TransmitFile來解決使用Response.BinaryWrite 下載超過400mb的檔案時導
Android Studio 新增依賴的幾種方式
1、庫依賴(library) 2、模組依賴(module) 以上兩種又可以分為內部和外部 a、內部庫 在工程的lib下直接貼上jar、aar檔案,androidstudio會自動載入指定目錄下的依賴庫。 即粘貼後,右鍵add as library。build.g
Android中實現IPC的幾種方式詳細分析及比較
1.使用Bundle ----> 用於android四大元件間的程序間通訊android的四大元件都可使用Bundle傳遞資料 所以如果要實現四大元件間的程序間通訊 完全可以使用Bundle來實現 簡單方便 2.使用檔案共享 ---->用於單執行緒讀寫
Android 開發之 定時器 Handler + Runnable方式
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.othe
目前開發手機app的幾種方式
是否採用這種模式,需要根據具體情況綜合考慮。不過在大多數場景下,如果你已經具備一定的web開發經驗,採用這種方式進入移動App開發領域,還是不錯的一個選擇。 畢竟,將產品低成本地開發出來並更快地推向市場,有時是最重要的事情。
java讀取寫入檔案幾種方式效率比較
public class ReadTxtJson {public static String readTxtFile(String FileName) throws Exception {BufferedInputStream bufferedInputStream = n