Derek Nowicki
Derek Nowicki

Reputation: 283

QML WebView Camera Permission

I'm working on a mobile app that has a video chat feature. I found a nice javascript library for webrtc, which seems to load just fine in QWebView but I need to give it permission to access the camera and microphone and I can't figure out how to do that. Is it possible? QWebEngineView has a handy signal and slot for that, but it's not supported for mobile.

Manifest permissions are not working as described here.

Corresponding Qt bug: cant access camera and mic with QML WebView

Any ideas?

Upvotes: 11

Views: 1939

Answers (3)

Swapnil
Swapnil

Reputation: 2502

We resolved this QT bug by recompiling QT code and updating corresponding jars. We downloaded QT Source code for Android using Maintenance tool. While going through QT source code we found that due to some reason Qt developers are not overriding onPermissionRequest() of WebCromeClientdue to which WebView is not allowing Media access to JS functions. Please follow following steps to resolve issue.

  1. Download QT Source code.

  2. Update code in QtAndroidWebViewController.java. This class is located at ~/Qt/5.10.0/Src/qtwebview/src/jar/src/org/qtproject/qt5/android/view. Add following function to inner class QtAndroidWebChromeClient.

    @Override public void onPermissionRequest(PermissionRequest request) { request.grant(request.getResources()); }

  3. Depending on your Android sdk you may need to Comment/Change deprecated functions for older versions of Android. Also you can change few of the methods in QtAndroidWebViewClient as per new versions of Android.

  4. Then import project ~/Qt/5.10.0/Src/qtwebview In your Qt creator, You may need some Java knowledge to resolve issues(if you get some build issues)

  5. Depending on your build folder path Settings, successful build project will generate two jars in path build_folder/jar QtAndroidWebView.jar, QtAndroidWebView-bundled.jar.

  6. Replace jars in path ~/Qt/5.10.0/android_armv7/jar/.

  7. Now rebuild(Clean build) your original project, which is using WebView and issue is Resolved.

    Note: Path may change depending on QT Download Path and Operating System, but built jar can be replaced on any System(as java jars).

    If QT resolves this issue in Next release we can replace Updated jars.

Upvotes: 3

Hubi
Hubi

Reputation: 1639

Qt has implemented Android's permission model in version 5.10.

See here: http://doc.qt.io/qt-5/qtandroid.html.

The functions you have to use are:

  • checkPermission
  • requestPermissions
  • requestPermissionsSync

If you want to access a camera and microphone you have to request the permissions before using. Example:

QStringList perms;
perms << "android.permission.CAMERA" << "android.permission.RECORD_AUDIO"; 
QtAndroid::checkPermissions(perms, [](const PermissionResultMap& resMap) {
     foreach(const QString &key, resMap.keys()) {
         qDebug() << "Result of permission" << key << ":" << resMap[key];
     }
}); 

Upvotes: 6

GensaGames
GensaGames

Reputation: 5788

Manifest permission doesn't work for Android 22>. So in your case, you need to access runtime permission. Performing this in Java.

if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an explanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                arrayOf(Manifest.permission.READ_CONTACTS),
                MY_PERMISSIONS_REQUEST_READ_CONTACTS)

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

See from Official Doc. But in your case, I think you need level via NDK, to call the same behavior. It's also described here.

Upvotes: 5

Related Questions