Android store recorded audio using input text?

2019-09-18 01:42发布

问题:

I have an app that records and stores the audio on the sdcard. It records and saves it in the right place, but saves it as android.widget.EditText@random number. I was using an EditText to get the filename.

Does anyone know how I can let the user enter some text and save the file as that. I thought I was onto something with the code below, but no...

try {
                    //Set user input as filename
                    String filename = getInput.toString();
                    // set the storage directory to SDcard
                    File storageDir = new File(Environment.getExternalStorageDirectory(), "audio");
                    storageDir.mkdir();
                    Log.d(APP_TAG, "Storage directory set to " + storageDir);

                    outfile = File.createTempFile(filename, ".3gp", storageDir);
                    //show filename
                    System.out.println("File is: "+outfile.getAbsolutePath());

                    // initiate recorder
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    recorder.setOutputFile(outfile.getAbsolutePath());

                } catch (IOException e) {
                    Log.w(APP_TAG, "File not accessible ", e);
                } catch (IllegalArgumentException e) {
                    Log.w(APP_TAG, "Illegal argument ", e);
                } catch (IllegalStateException e) {
                    Log.w(APP_TAG, "Illegal state, call reset/restore", e);
                }
                startRecord();
            } else {
                stopRecord();

here is my updated code:

    //set filename to user input
                String filename = getInput.getText().toString()+".3gp";
                // show filename
                System.out.println("File is: " + filename);

                // initiate recorder
                recorder = new MediaRecorder();
                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                recorder.setOutputFile("/sdcard/audio/"+filename);
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);                    
                player.setDataSource(filename);

回答1:

android.widget.EditText@random number indicates that your getInput is a EditText, So to get its text you need to call its getText() Method, like below,

String filename = getInput.getText().toString();