qr-scanner icon indicating copy to clipboard operation
qr-scanner copied to clipboard

Android Webview support

Open markaw375 opened this issue 2 years ago • 3 comments

Hi,

Did someone use it in Android webview? It is showing black screen and doesn't seem to work.

Thanks, Mark

markaw375 avatar Apr 14 '23 13:04 markaw375

I really need to start searching issues before I start using stuff! Is this true? Was any fix ever found @markaw375 ?

rocket-pig avatar Sep 08 '23 00:09 rocket-pig

Hi,

Did someone use it in Android webview? It is showing black screen and doesn't seem to work.

Thanks, Mark

You need to give camera permission for Android devices.

jewelsonn avatar Oct 22 '23 14:10 jewelsonn

I was encountering the same issues with the WebView using the camera resource.

I resolved this by adding the permissions in the AndroidManifest.xml file:

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

In the Activity, I added the following code to handle the camera usage permission:

class MainActivity : AppCompatActivity() {
    private lateinit var webView: WebView
    private val REQUEST_CAMERA_PERMISSION = 100

    companion object {
        private const val REQUEST_CODE_NOTIFICATION_PERMISSION = 1
        private const val PREFS_NAME = "NotificationPermissionPrefs"
        private const val NOTIFICATION_PERMISSION_STATUS = "notification_permission_status"
        const val PERMISSION_DENIED = -1
        const val PERMISSION_NOT_ASKED = 0
        const val PERMISSION_GRANTED = 1
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        webView = findViewById(R.id.webview)
        webView.webViewClient = WebViewClient()

        // Enable JavaScript
        webView.settings.javaScriptEnabled = true
        webView.addJavascriptInterface(WebAppInterface(this, webView), "Android")

        webView.webChromeClient = object : WebChromeClient() {
            override fun onPermissionRequest(request: PermissionRequest) {
                runOnUiThread {
                    request.grant(request.resources)
                }
            }
        }

        val urlParam = intent.getStringExtra("url")
        webView.loadUrl(urlParam)

        webView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
            }
        }

        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION)
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            REQUEST_CAMERA_PERMISSION -> {
                if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    // Permission was granted
                } else {
                    // Permission was denied. Disable the functionality that depends on this permission.
                    Toast.makeText(this, "Camera permission is required to use this feature", Toast.LENGTH_SHORT).show()
                }
                return
            }
            else -> {
                // Ignore all other requests.
            }
        }
    }
}

In this sample, the MainActivity class is an AppCompatActivity that sets up a WebView and requests camera permissions if they are not already granted. The onPermissionRequest method in the WebChromeClient is overridden to grant all requested permissions. The onRequestPermissionsResult method is overridden to handle the result of the camera permission request.

renan-rmp avatar Apr 22 '24 00:04 renan-rmp