-1

I am developing a web app which I would now like to make an android app for. For testing, a simple WebView is enough. Now the issue is that whenever a regular desktop web browser asks for camera and microphone permissions, the app does nothing. It doesn't crash but it also doesn't join the stream.

What do I have to do in order to fix this?

So far, I have the following Code:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.techadvice">

    <uses-permission  android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    <uses-permission android:name="android.webkit.PermissionRequest" />
    <uses-feature android:name="android.hardware.camera"/>

    <application

        android:usesCleartextTraffic="true"

        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java:

package com.example.techadvice;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;

import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView=(WebView) findViewById(R.id.webview);

        WebSettings webSettings=myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowFileAccessFromFileURLs(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        myWebView.getSettings().setCacheMode(webSettings.LOAD_NO_CACHE);

        myWebView.setWebViewClient(new WebViewClient());

        myWebView.loadUrl("%some url%");
    }

    public class mywebClient extends WebViewClient{
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon){
            super.onPageStarted(view,url,favicon);
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view,String url){
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public void onBackPressed(){
        if(myWebView.canGoBack()) {
            myWebView.goBack();
        }
        else{
            super.onBackPressed();
        }
    }
}

I was mostly following this tutorial: https://youtu.be/2cWbepS1NZM

Thanks in advance!

  • 1
    Well the first thing you gotta do is to [ask for camera permission](https://stackoverflow.com/questions/40659198/how-to-access-the-camera-from-within-a-webview) once it allowed then only you can proceed further . – ADM May 01 '22 at 11:03

1 Answers1

0

In your onCreate you can check for permission and request it if the permission hasn't been granted already. You can try something like this:

int MY_PERMISSIONS_REQUEST_CAMERA = 0;

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, MY_PERMISSIONS_REQUEST_CAMERA);

        }