gpt4 book ai didi

java - Android webview摄像头和麦克风权限已授予但无法访问

转载 作者:行者123 更新时间:2023-12-02 02:36:28 25 4
gpt4 key购买 nike

我是这个 Android webview 的新手。我正在创建的应用程序可以访问相机和麦克风。

相机麦克风的权限已获得,但当尝试打开相机时,它没有打开。

该网站在移动设备和桌面浏览器中都能正常运行,但在我的应用程序中无法打开。请帮我解决这个问题。我有什么遗漏的吗?

@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 中

Failed to notify a WebView W/VideoCapabilities: Unrecognized profile 2130706434 for video/avc W/VideoCapabilities: Unsupported mime video/divx W/VideoCapabilities: Unsupported mime video/divx4 W/VideoCapabilities: Unsupported mime video/mp4v-esdp W/VideoCapabilities: Unsupported mime video/mp4v-esdp I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es I/CameraManagerGlobal: Connecting to camera service W/cr_media: Requires MODIFY_AUDIO_SETTINGS and RECORD_AUDIO. No audio device will be available for recording W/cr_media: Requires MODIFY_AUDIO_SETTINGS and RECORD_AUDIO. No audio device will be available for recording

最佳答案

您不能直接在 webView 中使用麦克风和摄像头,因为您需要添加额外的权限 MODIFY_AUDIO_SETTINGS 和 RECORD_AUDIO,然后只有您才能访问这些功能。

使用以下命令在 list 文件中添加这些权限

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

然后在您的情况下使用 webView 客户端类中的 onPermissionRequest 方法请求许可,即 MyCustomWebChromeClient

关于java - Android webview摄像头和麦克风权限已授予但无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57200229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com