在我的Android应用中,我录制了一个文件。 wav在这种模式下:
public class MainActivity extends ActionBarActivity {
private static final int RECORDER_BPP = 16;
private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
private static final String AUDIO_RECORDER_FOLDER = "Audio";
private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
private static final int RECORDER_SAMPLERATE = 8000;
private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_MONO;
private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
private static final String String = null;
short[] audioData;
private static AudioRecord recorder = null;
private static int bufferSize = 0;
private Thread recordingThread = null;
private boolean isRecording = false;
/*Complex[] fftTempArray;
Complex[] fftArray;*/
int[] bufferData;
int bytesRecorded;
TextView tv;
private Button ca;
File f2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_audio);
setButtonHandlers();
enableButtons(false);
bufferSize = AudioRecord.getMinBufferSize(8000,AudioFormat.CHANNEL_IN_MONO,AudioFormat.ENCODING_PCM_16BIT);
audioData = new short [bufferSize]; //short array that pcm data is put into.
tv = (TextView)findViewById(R.id.textView1);
ca = (Button)findViewById(R.id.button2);
}
private void setButtonHandlers() {
((Button)findViewById(R.id.button1)).setOnClickListener(btnClick);
((Button)findViewById(R.id.btStop)).setOnClickListener(btnClick);
}
private void enableButton(int id,boolean isEnable){
((Button)findViewById(id)).setEnabled(isEnable);
}
private void enableButtons(boolean isRecording) {
enableButton(R.id.button1,!isRecording);
enableButton(R.id.btStop,isRecording);
}
private String getFilename(){
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_HHmm");
String strDate = sdf.format(c.getTime());
if(!file.exists()){
file.mkdirs();
}
String fileaudio= new String("record");
f2= new File(file.getAbsolutePath() + "/" + fileaudio + "-" +strDate +AUDIO_RECORDER_FILE_EXT_WAV);
return (file.getAbsolutePath() + "/" + fileaudio + "-" +strDate +AUDIO_RECORDER_FILE_EXT_WAV);
}
private String getTempFilename(){
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
if(!file.exists()){
file.mkdirs();
}
File tempFile = new File(filepath,AUDIO_RECORDER_TEMP_FILE);
if(tempFile.exists())
tempFile.delete();
return (file.getAbsolutePath() + "/" + AUDIO_RECORDER_TEMP_FILE);
}
private void startRecording(){
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);
recorder.startRecording();
isRecording = true;
recordingThread = new Thread(new Runnable() {
public void run() {
writeAudioDataToFile();
}
},"Audio Thread");
recordingThread.start();
}
private void writeAudioDataToFile(){
......}
private void stopRecording(){
if(null != recorder){
isRecording = false;
recorder.stop();
recorder.release();
recorder = null;
recordingThread = null;
}
copyWaveFile(getTempFilename(),getFilename());
}
private void deleteTempFile() {
File file = new File(getTempFilename());
file.delete();
}
private void copyWaveFile(String inFilename,String outFilename){
......
}
private void WriteWaveFileHeader(
......
}
private View.OnClickListener btnClick = new View.OnClickListener() {
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:{
enableButtons(true);
startRecording();
.......
}
};
};
现在,我想在我点击按钮时打开的另一个Activity中使用此录制的文件。出于这个原因,我创建了“更改”方法 我记得在bott的onclick()中。在这个方法中,我想改变“活动”并传递文件路径。这是我的代码:
public void change (View view){
Intent changeActivity;
changeActivity = new Intent (this, SecondActivity.class);
startActivity(changeActivity);
String filepath = Environment.getExternalStorageDirectory().getPath();
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy_HHmm");
String strDate = sdf.format(c.getTime());
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
File f2= new File(file.getAbsolutePath() + "/" + "record" + "-" +strDate +AUDIO_RECORDER_FILE_EXT_WAV);
String path = f2.getAbsolutePath();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("lname", path);
startActivity(intent);
}
}
在第二个活动中,我以这种模式回忆起文件:
Intent intent = getIntent();
lName = intent.getStringExtra("lname");
File storage = Environment.getExternalStorageDirectory();
File file = new File(storage,lName);
此代码无法正常工作,因为文件路径没有通过。为什么?有人能帮助我吗?
答案 0 :(得分:2)
因为您要从上一个Activity发送文件的绝对路径,所以lName
包含完整路径,包括文件而不是文件名,因此使用File类构造函数创建文件,该参数将一个参数作为绝对文件路径:
File file = new File(lName);