Search code examples
androidandroid-activityandroid-camera-intent

Android - Start CameraIntent activity for result crash without informations in emulator


I create an app that use camera. So I have a CameraIntent started as activity. startActivity(cameraIntent) works fine on every device and emulator I have, but when I use startActivityForResult(cameraIntent, 1), I've got a CameraApp crash on emulator with Pixel 2 and Pixel 3XL API 29. I tried on my real huawei device and on another emulator (nexus API 30) and it's working. I cannot find any informations in logcat so I don't know where to look at to find the reason of the bug. I guess it's API 29 (or Pixel emulator ?) but I don't find any informations about this problem yet.

EDIT: I tried many versions and devices but this bug only happen on Pixel 2 with API 29. Does anyone have an idea of why ?

This is the code of the app I'm using to replicate the bug: MainActivity.kt


    private lateinit var button: Button

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

        button = findViewById(R.id.button)

        button.setOnClickListener {
            val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)

            if (packageManager.resolveActivity(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY) == null) {
                Toast.makeText(this, "no camera", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }

            startActivityForResult(cameraIntent, 1)
        }

    }

activity_main.xml (only a simple button)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

(Sorry about my english.) Thanks you for your help !


Solution

  • make sure that camera permission is attach in manifest file and permission granted by user here is the code of permission:

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

    if enable multiple permission then prefer you to read the docmentation and use this dependency :

    implementation 'com.karumi:dexter:6.2.1'