標籤:android http io os ar for strong sp 檔案
amr格式的檔案頭是6位元組,在進行檔案合并的時候要減去除第一個檔案以外的其他檔案的檔案頭。下面介紹合并檔案的幾種方式,並通過合并amr檔案來舉例介紹合并檔案的具體流程。
注意:不同檔案的檔案頭是不一樣的,所以在合并的時候根據不同檔案相應的減去合并檔案的檔案頭。具體你可以學習Android開發教程。
步驟一:擷取要合并的檔案及建立合并後儲存的檔案
/**用於存放要合并的檔案的集合**/
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();
}
}
還有更多移動互連網教程資訊等請關注e良師益友網。
Android合并檔案的三種方式代碼