Question

hi i have to work in WebView with Camera. I am implemented some HTML code that file load from the Assets in WebView and my HTML file contains a Button for uploading image into server. Images are selected from Gallery itself and taking photo from camera. but when i try with camera it's not opening. I dont know why? also, i added required camera permission in my manifest.xml.

please some one point me where i am going wrong?

Manifest.xml

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

<permission
    android:name="com.example.maptest.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.example.maptest.permission.MAPS_RECEIVE" />
<!-- Copied from Google Maps Library/AndroidManifest.xml. -->
<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />


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

<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- External storage for caching. -->
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:debuggable="true"
    android:icon="@drawable/sms_lg"
    android:label="SMS Reader" >
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="mapkey" />

    <activity
        android:name=".WebView2"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="BasicMapActivity2" >
    </activity>
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
</application>

</manifest>
Was it helpful?

Solution 2

I got your issue i think you need to add <uses-feature> for camera like:

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />

It's becoz requesting the permission grants your application access to the appropriate hardware and software, while declaring the features used by your application ensures proper device compatibility.

Try this and give me feedback on this. For more information go to http://developer.android.com/guide/topics/manifest/uses-feature-element.html

OTHER TIPS

If you want to use the camera in your website with (html5):

<input type="file" accept="image/*" capture="camera">

First of all add this line into your Manifest:

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

Then you need to config you webview in android like this:

  • enable java script
  • enable the DOM storage
  • PermissionRequest (This class defines a permission request and is used when web content requests access to protected resources)

    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setDomStorageEnabled(true);
    webview.getSettings().setPluginState(WebSettings.PluginState.ON);
    
    webview.setWebChromeClient(new WebChromeClient(){
            // Need to accept permissions to use the camera
            @Override
            public void onPermissionRequest(final PermissionRequest request) {
                L.d("onPermissionRequest");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                     request.grant(request.getResources());
                }
            }
        });
    

I did what @manuel suggested, but still, I couldn't get it to work on looking into the log I found we also had to add

webView.getSettings().setMediaPlaybackRequiresUserGesture(false);

I was facing issue of not opening camera even if permissions were requested and granted. The output from camera was blank - white screen.

The problem was layer type property set into webView object.

mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

After changing LAYER_TYPE_SOFTWARE into LAYER_TYPE_NONE the problem has gone.

mWebView.setLayerType(View.LAYER_TYPE_NONE, null);

I hope that someone this helps.

Starting with Android 5.0, they introduced the PermissionRequest class, which allows your app to grant the WebView permission to access protected resources like the camera and microphone, through web APIs such as getUserMedia(). Your app must have the appropriate Android permissions for these resources in order to grant the permissions to the WebView.

I had tried all solutions but in the end you have to ask it manually. Check for Permission

val permission = Manifest.permission.CAMERA
                    val grant = ContextCompat.checkSelfPermission(this@WebViewActivity, permission)
                    if (grant != PackageManager.PERMISSION_GRANTED) {
                        val permission_list = arrayOf(permission)
                        ActivityCompat.requestPermissions(this@WebViewActivity, permission_list, 1);
                                           }

<input type="file" accept="image/*" capture="camera" />

This will not work in android studio because it is not compatible with android studio SDK abut it is available for chrome. You can redirect your page from app to chrome and then to the app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top