Can't import import android.hardware.camera2 classes

I imported the opencv android library to my android studio and the Camera2Renderer class has a lot of compiler errors because the android.hardware.camera2 classes can’t be imported.

enter image description here

  • How to get return value from javascript in webview of android?
  • How to include suggestions in Android Keyboard
  • What triggers a View's measure() to be called
  • This client ID is globally unique and is already in use
  • Bluetooth LE ScanFilters don't work on Android M
  • How to add more shortcuts to android studio toolbar?
  • Related posts:

    Android, Finish all activities
    How can I specify a null as an XML attribute for Android?
    Post data to webview in android
    RxJava + Retrofit -> BaseObservable for API calls for centralized response handling
    How to reliably determine the width of a multi line string?
    What happens when App crashes?
  • Android Studio with Google Play: Google Play services is missing
  • Examples for Android Launch modes
  • Which one to use NavUtils.navigateUpFromSameTask() vs. onBackPressed()
  • How to change vibrate mode to ringer prgrammatically through audio manager
  • Punch a hole in a rectangle overlay with HW acceleration enabled on View
  • Display view above status bar?
  • 2 Solutions collect form web for “Can't import import android.hardware.camera2 classes”

    I solved the problem. Jim was right, i did not have the correct target API. For the next person who has this problem and finds this thread, the solution is that you have to make sure that the build.gradle files for your project and your openCV match. Thanks to everyone who helped me, and Merry Christmas!

    i am working with openCVLibrary3.2.0 and trying to run its sample and faced same issue but i changed gradle files for both mysampleApp and openCVLibrary320 module as below

    This is my app build.gradle

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
    
        defaultConfig {
            applicationId "org.opencv.face"
            minSdkVersion 9
            targetSdkVersion 21
            compileOptions {
                sourceCompatibility JavaVersion.VERSION_1_5
                targetCompatibility JavaVersion.VERSION_1_5
            }
    
            ndk {
                moduleName "native_sample"
            }
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
        externalNativeBuild {
            ndkBuild {
                path 'src/main/jni/Android.mk'
            }
        }
    }
    
    dependencies {
        compile project(path: ':openCVLibrary320')
    }
    
    and this my OpenCV library module build.gradle file
    
    apply plugin: 'com.android.library'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"
    
        defaultConfig {
            minSdkVersion 9
            targetSdkVersion 21
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
    } 
    

    Note:
    things to notice are that compileSdkVersion,buildToolsVersion,minSdkVersion, targetSdkVersion these must be same for all gradle files

    i changed the compileSdkVersion from 14 to 23
    and buildToolsVersion to “23.0.2”
    this solved the camera2 import related issue

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.