intent.resolveActivity() is NULL on a device with the camera (4.2.2)
Asked Answered
S

3

5

Using this piece of code is suggested and it states

Notice that the startActivityForResult() method is protected by a condition that calls resolveActivity(), which returns the first activity component that can handle the intent. Performing this check is important because if you call startActivityForResult() using an intent that no app can handle, your app will crash. So as long as the result is not null, it's safe to use the intent.

Code:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

It works fine, but NOT on an Alcatel device running 4.2.2. I get the ELSE message with my toast "Cannot use camera..."

How is this possible if a device has the camera?

What should I do now? How to handle this use case - obviously I have to rely that some devices with the camera will report "cannot use camera", detect that those devices have camera and then use it?!

The documentation or SO questions say nothing about the ELSE case.

Stiegler answered 3/6, 2016 at 17:50 Comment(0)
S
8

How is this possible if a device has the camera?

The authors of the camera app pre-installed on that device failed to support the ACTION_IMAGE_CAPTURE Intent action. There are lots of bugs with camera apps.

While probably not relevant for your situation here, bear in mind that with restricted profiles on Android 4.3+, the current user of the device might not have access to an ACTION_IMAGE_CAPTURE activity, even if the device happens to have one.

What should I do now?

Do something like:

  • Tell the user that they do not have a compatible camera app and steer them towards downloading one that you know works well with your app

  • Do not rely upon ACTION_IMAGE_CAPTURE and instead embed photo-taking capability in your app

  • Disable the feature of your app that requires ACTION_IMAGE_CAPTURE, on devices where resolveActivity() returns null

  • Etc.

Sateia answered 3/6, 2016 at 18:9 Comment(3)
OMG and this is its stock camera app. Could it really be that they never supported this intent??? OMGStiegler
@sandalone: Perhaps this device does not come with the Play Store and other Google proprietary apps, in which case it does not have to meet any particular compatibility standards. Or, perhaps ACTION_IMAGE_CAPTURE was not part of the Compatibility Test Suite back when this device was made. Or, perhaps it passed the test suite when it shipped, but then Alcatel broke the app in a firmware upgrade (which, AFAICT, does not get re-tested).Sateia
It has Play Store (EU location). So it got to be that something broken but never tested.Stiegler
K
5

Please check have you use permission in AndroidManifest.xml

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

if you use then use this edited code to check is there is camera or not..

private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (getApplicationContext().getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_CAMERA)) {
        // this device has a camera
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    } else {
        // no camera on this device
    }

}

if you want to use your previous code then don't forget to set this code in AndroidManifest.xml

    <uses-feature android:name="android.hardware.camera"
              android:required="true" />
Kekkonen answered 3/6, 2016 at 18:10 Comment(3)
Why is the bold important? As I want devices without camera to use app (to load images from the Gallery or sdcard)??? I cannot find anywhere that without the bold code will generate exceptionStiegler
to load images from the Gallery or sdcard is a different issue. CAPTURE and PICK not same. To open Gallery image generally we use Intent.ACTION_PICK and user permission is also different red storage permission. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />Kekkonen
That is not what I asked. Why do we need required=true? I want devices without camera to use the app.Stiegler
T
1

i had the same issue on nokia and motorola devices, turns out you dont actually have to verify permissions to access camera, once permissions are granted, it just works

this is MY CODE on the app im making, i hope it helps

enter code here    private void tomarfoto() throws IOException {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//Activar la opcion de camara
    Log.d("camara","opcion camara activada");

   try {
           File photoFile = null;
           photoFile = createImagenFile();
           Log.d("camara", "archivo de imagen creado");
           if (photoFile != null) {
               Uri outputFileUri = FileProvider.getUriForFile(this, MY_PROVIDER, photoFile);
               cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); // Enviar la imagen como parametro y almacenarla
               startActivityForResult(Intent.createChooser(cameraIntent, "Selecciona la cámara"), COD_FOTO); // Toma la foto
               Log.d("camara", "foto tomada");
           }


   }catch(IOException e){
       Log.d("Exception",e.getMessage());
   }
}
Tangram answered 25/7, 2022 at 23:35 Comment(1)
I think you should just skip "if(cameraIntent.resolveActivity(getPackageManager()) != null)" when you're targeting android R (API 30) and above, I tried it and It worked.Shrine

© 2022 - 2024 — McMap. All rights reserved.