Android: 在两个活动之间传递文件路径

Android: pass filepath between two Activities

在我的 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 中使用这个录制的文件。为此,我创建了 "change" 方法 我记得在按钮的 onclick() 中。在这种方法中,我想更改 "activity" 并传递文件路径。这是我的代码:

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


}

在第二个Activity中,我记得这个模式下的文件:

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

这段代码不能正常工作,因为没有文件路径的通道。为什么?有人可以帮助我吗?

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

File file = new File(lName);