Android: transfer file path between two actions

in my android app i am writing 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 Office, which I open when I click the button. For this reason, I created a โ€œchangeโ€ method that I recall in onclick () from botton. In this method, I want to change the "activity" and transfer the path to the file. 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 a file in this mode:

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

This code does not work fine because there is no file path. What for? Can anybody help me?

+4
source share
1 answer

, lName , , , , File, :

File file = new File(lName);
+2

Source: https://habr.com/ru/post/1570842/


All Articles