简体   繁体   中英

Permission problems for sound recording

I am having a couple of problems recording sound on a device. The code I am using if from the android dev site ( Site Link ) and is as follows:

    public void onClickStart(View v) throws IllegalStateException, IOException{
        startRecord();
    }

    public void onClickStop(View v) throws IllegalStateException, IOException{
        stopRecord();
    }

    private void startRecord() throws IllegalStateException, IOException{
        recorder = new MediaRecorder(); 
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);  //ok so I say audio source is the microphone, is it windows/linux microphone on the emulator? 
        recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 
        recorder.setOutputFile("/sdcard/test.3gpp"); 
        recorder.prepare(); 
        recorder.start();  
    }

    private void stopRecord(){
        recorder.stop();
//      recorder.release();
    }

With 2 buttons in the main layout which it turn both stop and start the recording (in theory that is).

But from LogCat when trying this out on my device (really cant be bothered with trying on the emulator) I get the following errors:

Error 1:
ERROR/MediaRecorder(14541): start called in an invalid state: 4
java.lang.IllegalStateException: Could not execute method of the activity
Caused by: java.lang.reflect.InvocationTargetException

Error 2:
Caused by: java.io.FileNotFoundException: /sdcard/test.3gpp (Permission denied)

And I also have the following permissions set in my Manifest.xml file:

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

傻错误在这里对不起,它没有用,因为我将手机安装在笔记本电脑上,因此无法实际到达sdcard。

对于错误2:请尝试静态使用getExternalStorageDirectory()而不是“ sdcard”,也许该路径在您的设备上无效/可访问。

To others that stumble upon this, the exception does not always tell you that the FileNotFoundException is associated with a denial of permission. Check that the permission WRITE_EXTERNAL_STORAGE exists on your AndroidManifest.xml. In the OP's case, it did.

Even if you create an external SD-Card file (using the mksdcard command) and associate it with your phone, the call to getExternalStorageDirectory() may still point to /sdcard (since the method's contract does not guarantee that it will return a non-internal storage location ). You can always use the 'adb shell' command to check if sdcard does exist. If you want to copy files to / from the sdcard, use the 'adb pull' / 'adb push' commands.

References:

http://developer.android.com/guide/developing/devices/emulator.html#sdcard

How to copy and edit files in Android shell?

writing to sdcard not working

Don't hardcore /sdcard/test.3gpp in output file, you should use getExternalStorage() use follow code:

boolean exists = (new File(android.os.Environment.getExternalStorageDirectory() + "/Record/")).exists();
if (!exists) {
    new File(android.os.Environment.getExternalStorageDirectory() + "/Record/").mkdirs();
}

and set this path to output file recorder.setOutputFile(android.os.Environment.getExternalStorageDirectory()+"/Record/test.3gp");

It's works for me

I'll bet some people are missing write permissions in the manifest.

Within the main manifest tag, please ensure you have the following permissions :

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

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