Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with MediaRecorder class to record audio - prepare() gives an exception - Permission denied

I'm new in Android development and I have the next question/problem.

I'm playing around with the MediaRecorder class to record just audio from the microphone. I'm following the steps indicated in the official site: http://developer.android.com/reference/android/media/MediaRecorder.html

So I have a method that initializes and configure the MediaRecorder object in order to start recording. Here you have the code:


        //initializes audio recorder
        MediaRecorder mrecorder = new MediaRecorder();
        //configure the input sources
        mrecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        //set the output format
        mrecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        //set the audio encoding
        mrecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
        //specify the output file
        mrecorder.setOutputFile("/sdcard/test.3gp");
        //prepare for recording
        try {
            mrecorder.prepare();
        } catch (IllegalStateException e) { 
            e.printStackTrace();
            Log.d("Syso". e.toString());
        } catch (IOException e) { 
            e.printStackTrace();
            Log.d("Syso". e.toString());
        }

When I execute this code in the simulator, thanks to logcat, I can see that the method prepare() gives an exception when is called:


java.io.FileNotFoundException: /sdcard/test.3gp (Permission denied)

And I have no idea why this is happening. Due to the message of the exception, I've given permissions in the manifest to access to storage by adding the following line to the xml:


<uses-permission android:name="android.permission.STORAGE" />

But this doesn't fix anything and I still get the same exception all the time. The SDCard is mounted according to the emulator, so I have no clue.

like image 238
arakn0 Avatar asked Feb 27 '23 21:02

arakn0


1 Answers

Add the WRITE_EXTERNAL_STORAGE permission to AndroidManifest.xml.

like image 140
JRL Avatar answered Apr 06 '23 14:04

JRL