我是这个Android webview
的新手。我正在创建的应用程序具有摄像头和麦克风访问权限。
已授予camera
和microphone
的权限,但是在尝试打开相机时,无法打开。
该网站在移动设备以及台式机浏览器中均能正常运行,但在我的应用中无法打开。请帮助我解决这个问题。有什么我想念的吗?
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == MY_PERMISSION_REQUEST_CAMERA || requestCode == MY_PERMISSIONS_REQUEST_RECORD_AUDIO) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "permission not granted", Toast.LENGTH_SHORT).show();
}
}
}
@SuppressLint({"SetJavaScriptEnabled", "ClickableViewAccessibility"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this.getApplicationContext();
if (!isTaskRoot()) {
finish();
return;
}
myWebView = findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebViewClient(new MyCustomWebViewClient());
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.setWebChromeClient(new MyCustomWebChromeClient());
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.loadUrl("https://www.skaplink.com");
myWebView.getSettings().setUserAgentString(userAgent);
/*String permission1 = Manifest.permission.CAMERA;
String permission2 = Manifest.permission.RECORD_AUDIO;
String permission3 = Manifest.permission.MODIFY_AUDIO_SETTINGS;
int grant1 = ContextCompat.checkSelfPermission(this, permission1);
int grant2 = ContextCompat.checkSelfPermission(this, permission2);
ContextCompat.checkSelfPermission(this, permission3);
if (grant1 != PackageManager.PERMISSION_GRANTED && grant2 != PackageManager.PERMISSION_GRANTED) {
String[] permission_list = new String[2];
permission_list[0] = permission1;
permission_list[1] = permission2;
ActivityCompat.requestPermissions(this, permission_list, 1);
ActivityCompat.requestPermissions(this, permission_list, 2);
Log.d("Granting Process", "Done");
}*/
permission();
}
void permission() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "You already granted the permission", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSION_REQUEST_CAMERA);
}
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, "You already granted the permission", Toast.LENGTH_SHORT).show();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
}
}
这些显示在我的logcat中
无法通知WebView W / VideoCapabilities:无法识别的配置文件
2130706434用于视频/ AVC W / VideoCapabilities:不支持的mime
video / divx W / VideoCapabilities:不支持的mime video / divx4
W / VideoCapabilities:不支持的mime video / mp4v-esdp
W / VideoCapabilities:不支持的mime video / mp4v-esdp
I / VideoCapabilities:视频/ mp4v-es不受支持的配置文件4
I / CameraManagerGlobal:连接到摄像头服务W / cr_media:
需要MODIFY_AUDIO_SETTINGS和RECORD_AUDIO。没有音频设备
可用于录制W / cr_media:需要MODIFY_AUDIO_SETTINGS
和RECORD_AUDIO。没有音频设备可用于录制
最佳答案
您不能直接将麦克风和摄像头与webView一起使用,因为您需要添加额外的权限MODIFY_AUDIO_SETTINGS和RECORD_AUDIO,然后只有您才能访问这些功能。
使用以下命令在清单文件中添加这些权限
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
然后在您的案例(
onPermissionRequest
)中使用webView客户端类中的MyCustomWebChromeClient
方法请求权限关于java - 已授予Android Webview摄像头和麦克风权限,但无法访问,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57200229/