resolveActivity method is returning null for Android 14

176 views Asked by At

I am facing a peculiar issue where resolveActivity returns null. I know there are similar questions already and solutions are provided, but I am specifically facing the issue for android 14. I have added the queries for the intent in my manifest file. Yet the issue only persists for android 14 devices.

Here is a snippet of my manifest file

<uses-feature android:name="android.hardware.camera" android:required= "true" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.
         android:icon="@drawable/logo"
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <queries>
        <intent>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
        </intent>
    </queries>

    <application
        android:name=".model.Multi_Dex"
        android:allowBackup="true"
        android:hardwareAccelerated="false"
...

And here is the snippet for the method used to launch the camera intent.

private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if(Build.VERSION.SDK_INT>33){
            takePictureIntent.setPackage(context.getPackageName());
        }
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                // Error occurred while creating the File
                ex.printStackTrace();
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.ft.fifthwheel.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                startActivityForResult(takePictureIntent, REQUEST_CAMERA);
            }
        }
    }
1

There are 1 answers

4
Zarar Mahmud On

Okay, so I have currently resolved this by using the queryIntentActivities method. I have fetched the list of ResolveInfo objects, created a new Intent for every intent mentioned in my manifest file and added each intent to a list of intents. Then I created a chooser Intent to trigger a choice of options to pick my photo, currently, I am only allowing the camera option to take the photo, but I might add more intents inside my queries in the manifest file.

Here is the sample code

private void dispatchTakePictureIntent() {
    File photoFile = null;
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    try {
        photoFile = createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File
        ex.printStackTrace();
    }
    if (photoFile != null) {
        Uri photoURI = FileProvider.getUriForFile(this,
                "com.ft.fifthwheel.fileprovider",
                photoFile);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
        List<Intent> intentList = new ArrayList<>();
        List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(takePictureIntent, 0);
        for (ResolveInfo resolveInfo : resInfo) {
            String packageName = resolveInfo.activityInfo.packageName;
            Intent targetedIntent = new Intent(takePictureIntent);
            targetedIntent.setPackage(packageName);
            intentList.add(targetedIntent);
        }
        Intent chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1), "Choose an Image");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
        // Ensure that there's a camera activity to handle the intent
        if (chooserIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            // Continue only if the File was successfully created
            chooserIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivityForResult(chooserIntent, REQUEST_CAMERA);
        }
    }
}

I would appreciate better solutions or improvements if there are any.