編輯:關於Android編程
下面介紹合並文件的幾種方式,並通過合並amr文件來舉例介紹合並文件的具體流程。amr格式的文件頭是6字節,所以在進行文件合並的時候要減去除第一個文件以外的其他文件的文件頭。
注意:不同文件的文件頭是不一樣的,所以在合並的時候根據不同文件相應的減去合並文件的文件頭。
步驟一:獲取要合並的文件及創建合並後保存的文件
/**用於存放要合並的文件的集合**/ ListtempFiles=new ArrayList (); /**合並之後的文件**/ 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方式
* */
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方式
* */
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中的使用
注解在android程序中的使用 何為注解: 在Java當中,注解又叫做“元數據”,它為我們在源代碼中添加信息提供了一種形式化的方法,讓我們能在以後的某個時間方便的使用這
Android 5.0 SharedElement 簡單應用分析(adp-activity-transitions)
好久沒寫點東西了,最近看到了一個轉場動畫比較酷炫,今天就來簡單滴分析一下。先看下今天的效果圖。分析下效果: 進入詳情頁的時候有共享元素,圓形動畫,文字部分的上移動畫,源碼
Android NDK 開發(筆記一)
接引:Android NDK r7以上集成了cywin,在開發NDK時不用那麼麻煩的在去下載與配置cywin.Android NDK 開發步驟:Eclipse 首先配置N
Android DrawerLayout帶有側滑功能的布局類(1)
DrawerLayout顧名思義就是一個管理布局的。使用方式可以與其它的布局類類似。DrawerLayout帶有滑動的功能。只要按照drawerLayout的規定布局方式