获取摄像头和麦克风权限_安卓6.0以下权限检测

这篇博客介绍了如何在安卓6.0以下的系统中检测和使用摄像头、麦克风以及存储权限。通过`isHasCameraPermission()`方法检查摄像头权限,`isHasAudioPermission()`方法检测录音权限,以及`isHasWritePermission()`方法测试存储写入权限。代码示例中包含了异常处理和资源释放。
摘要由CSDN通过智能技术生成

/**

* 测试当前摄像头能否被使用

*

* @return

*/

public static boolean isHasCameraPermission() {

boolean canUse = true;

Camera mCamera = null;

try {

mCamera = Camera.open(0);

mCamera.setDisplayOrientation(90);

} catch (Exception e) {

e.printStackTrace();

canUse = false;

}

if (canUse) {

mCamera.release();

mCamera = null;

}

return canUse;

}/**

* 判断是是否有录音权限

*/

public static boolean isHasAudioPermission() {

// 音频获取源

int audioSource = MediaRecorder.AudioSource.MIC;

// 设置音频采样率,44100是目前的标准,但是某些设备仍然支持22050,16000,11025

int sampleRateInHz = 44100;

// 设置音频的录制的声道CHANNEL_IN_STEREO为双声道,CHANNEL_CONFIGURATION_MONO为单声道

int channelConfig = AudioFormat.CHANNEL_IN_STEREO;

// 音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。

int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

// 缓冲区字节大小

int bufferSizeInBytes = 0;

bufferSizeInBytes = 0;

bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRateInHz,

channelConfig, audioFormat);

AudioRecord audioRecord = new AudioRecord(audioSource, sampleRateInHz,

channelConfig, audioFormat, bufferSizeInBytes);

//开始录制音频

try {

// 防止某些手机崩溃,例如联想

audioRecord.startRecording();

} catch (IllegalStateException e) {

e.printStackTrace();

}

/**

* 根据开始录音判断是否有录音权限

*/

if (audioRecord.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING) {

return false;

}

audioRecord.stop();

audioRecord.release();

audioRecord = null;

return true;

}

存储的权限检测/**

* 测试是否存储权限

*

* @return

*/

public static boolean isHasWritePermission() {

boolean canUse = true;

try {

FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + File.separator + "test.txt");

} catch (FileNotFoundException e) {

e.printStackTrace();

canUse = false;

}

return canUse;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值