简体   繁体   中英

Android: pass filepath between two Activities

in my Android app I record a file. wav in this mode:

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();

            .......
            }
        };
    };

Now, I want to use this recorded file in another Activity that I open when I click on a button. For this reason, I have created the "change" method which I recall in onclick() of the botton. In this method I want to change "activity" and to pass the filepath. This is my code:

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);
    }


}

In Second Activity, I recall the file in this mode:

Intent intent = getIntent();
lName = intent.getStringExtra("lname");
File storage = Environment.getExternalStorageDirectory();
File file = new File(storage,lName);

This code doesn't work fine because there isn't the passage of the filepath. Why? Can someone help me?

因为您从前一个Activity发送文件的绝对路径所以lName包含完整路径,包括文件而不是文件名,因此使用File类构造函数创建文件,该文件类将一个参数作为绝对文件路径:

File file = new File(lName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM