19

Android opencv samples and tutorials were running fine and suddenly one day I get this for all of those:
"It seems that your device does not support camera (or it is locked). The application will be closed"
Please help, how to I can fix it?

I have reinstalled opencv and imported again and made new emulators but the problem still persists.

Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
SBM
  • 201
  • 1
  • 2
  • 3

6 Answers6

37

Go to your device settings -> apps -> YOUR APP -> Permissions -> turn on camera permission..

Worked for me..

orimen
  • 571
  • 4
  • 11
19

Check the camera permission in AndroidManifest.xml.

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

    <uses-feature android:name="android.hardware.camera"/>
    <uses-feature android:name="android.hardware.camera.autofocus"/>
    <uses-feature android:name="android.hardware.camera.front"/>
    <uses-feature android:name="android.hardware.camera.front.autofocus"/>

Its working for me..

Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
11

From the Android Docs:

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app.

It means that on Android 23 or above, besides the manifest, you need to request permission on runtime as well. In this case, camera access.

To do this, you can use the code below:

// First check android version 
if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
//Check if permission is already granted
//thisActivity is your activity. (e.g.: MainActivity.this)
    if (ContextCompat.checkSelfPermission(thisActivity,
                    Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {

        // Give first an explanation, if needed.
        if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                Manifest.permission.CAMERA)) {

            // 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,
                    new String[]{Manifest.permission.CAMERA},
                    1);
        }
    }
}

You can also handle the request response, as described on the docs.

Hope it helps!

Geraldo Neto
  • 3,670
  • 1
  • 30
  • 33
  • Where do I add this code? This doesnt seem to be jhava code at all, getting many errors when I try to paste it somewhere in MainActivity, please clarify where to add – Mich Feb 25 '23 at 00:21
  • Hi, Mich! It is Java. I suggest you to read the docs linked on the answer so that you get more detailed and updated information. – Geraldo Neto Feb 26 '23 at 01:30
2

Just had this problem and I solved it by killing any other apps that were using the camera. I had some previous tutorials still running in the background.

Ben Trengrove
  • 8,191
  • 3
  • 40
  • 58
2

In my case, the issue was
My application use Android Camera in another activity
And another activity was not release the Camera after use it on Destroyed (lock it)
And after I release the Camera on the another activity, this dialog will not showed again.

So generally to fix this issue

  1. Check CAMERA permissions
  2. Check CAMERA not locked (by releasing it after usage in any other activities)
Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
  • related question about release https://stackoverflow.com/questions/11495842/how-surfaceholder-callbacks-are-related-to-activity-lifecycle – Ahmed Nabil Jan 01 '19 at 05:46
0

The samples should work because they are using the JavaCamera. I get this problem when I tried to use the Native one. It seems to be that the native one doesn't work for ervery phone. see this.

I have to add that in some devices the openCV native camera doesn't work at all , bug 2359.

Community
  • 1
  • 1
ahmed_khan_89
  • 2,755
  • 26
  • 49
  • I faced a similar issue when running on emulator, and the solution was: Go emulator, `Setting->Apps->my_app->Permissions` and then enable "Camera". I found it being requested but not enabled. – khkarens Nov 24 '15 at 19:32