java 录音权限问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4678901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Permission problems for sound recording
提问by SamRowley
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:
我在设备上录制声音时遇到了一些问题。我使用的代码来自 android 开发站点(站点链接),如下所示:
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).
在主布局中有 2 个按钮,它既可以停止又可以开始录制(理论上是这样)。
But from LogCat when trying this out on my device (really cant be bothered with trying on the emulator) I get the following errors:
但是从 LogCat 在我的设备上尝试这个时(真的不能在模拟器上尝试)我收到以下错误:
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:
我还在 Manifest.xml 文件中设置了以下权限:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
回答by SamRowley
Silly Error here sorry, it didn't work because I had the phone mounted on my laptop so it couldn't actually reach the sdcard.
这里是愚蠢的错误,抱歉,它不起作用,因为我将手机安装在笔记本电脑上,因此它实际上无法到达 SD 卡。
回答by Select0r
For error 2: try to use getExternalStorageDirectory()
instead of "sdcard" statically, maybe that path is not valid/reachable on your device.
对于错误 2:尝试getExternalStorageDirectory()
静态使用而不是“sdcard”,也许该路径在您的设备上无效/无法访问。
回答by Deepak Bala
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.
对于偶然发现此问题的其他人,异常并不总是告诉您 FileNotFoundException 与拒绝许可相关联。检查您的 AndroidManifest.xml 上是否存在 WRITE_EXTERNAL_STORAGE 权限。在 OP 的情况下,确实如此。
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.
即使您创建了一个外部 SD 卡文件(使用 mksdcard 命令)并将其与您的手机相关联,对 getExternalStorageDirectory() 的调用仍可能指向 /sdcard(因为该方法的约定不保证它将返回一个非-内部存储位置)。您始终可以使用“adb shell”命令来检查 sdcard 是否存在。如果要将文件复制到/从 sdcard,请使用“adb pull”/“adb push”命令。
References:
参考:
http://developer.android.com/guide/developing/devices/emulator.html#sdcard
http://developer.android.com/guide/developing/devices/emulator.html#sdcard
回答by Vicky
Don't hardcore /sdcard/test.3gpp
in output file, you should use getExternalStorage()
use follow code:
不要/sdcard/test.3gpp
在输出文件中硬核,您应该getExternalStorage()
使用以下代码:
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");
并将此路径设置为输出文件
recorder.setOutputFile(android.os.Environment.getExternalStorageDirectory()+"/Record/test.3gp");
It's works for me
它对我有用
回答by Avram Score
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"/>