2

I want the user to be able to quickly open the default camera app (or one they have set as default) in my app. However, I do not want to use android.media.action.IMAGE_CAPTURE, as this will only show the photo taking portion of the app. I just want to simply open the camera app without using this. I do know that this is possible, as several gallery apps that I have used (Most notibale: Focus) have been able to just simply open the camera app with no issue, and did not use IMAGE_CAPTURE.

pancodemakes
  • 564
  • 7
  • 26
  • Not sure but you might just use the `android.media.action.IMAGE_CAPTURE` intent to get a list of all apps that support that intent and then just filter the list and launch one of them using it's package's default intent. – Jan Heinrich Reimer Jun 01 '17 at 01:59
  • I tried that, but it only shows the actual image capture intent instead of opening the app as a whole. No matter what camera app is opened, it will only open the image capture section. – pancodemakes Jun 01 '17 at 02:04
  • Added a more detailed explanation as an answer. – Jan Heinrich Reimer Jun 01 '17 at 03:15

4 Answers4

4

This can be achieved by using PackageManager#resolveActivity(Intent)

In Kotlin:

val info: ResolveInfo? = packageManager
        .resolveActivity(cameraIntent);
if (info == null) {
    // No camera app installed.
    return
}

// Documentation says at least one of the three infos is not-null:
val app: ApplicationInfo = info.activityInfo?.applicationInfo 
        ?: info.serviceInfo?.applicationInfo 
        ?: info.providerInfo!!.applicationInfo 
val launch: Intent? = packageManager
        .getLaunchIntentForPackage(app.packageName)
if (launch == null) {
    // Camera app has no default intent.
    return
}
// Launch the camera intent's 
// resolved app's default activity.
context.startActivity(launch)

(Where cameraIntent is the Intent created using the android.media.action.IMAGE_CAPTURE filter, context is the current app context and packageManager is the context's PackageManager instance.)

2

If you want to just open default camera app, use the code below

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        startActivity(
                getPackageManager().getLaunchIntentForPackage(

intent.resolveActivity(getPackageManager()).getPackageName()));
Izya Pitersky
  • 106
  • 1
  • 6
0
       String intentpackage;
        List<ApplicationInfo> list = packageManager.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
            for (int a=0;a<list.size();a++) {
                if((list.get(n).flags & ApplicationInfo.FLAG_SYSTEM)==1)
                {
                    Log.d("TAG", "Installed Applications  : " + list.get(a).loadLabel(packageManager).toString());
                    Log.d("TAG", "package name  : " + list.get(a).packageName);
                    if(list.get(a).loadLabel(packageManager).toString().equalsIgnoreCase("Camera")) {
                        intentpackage = list.get(a).packageName;
                        break;
                    }
                }
            }

 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if(defaultCameraPackage!=null){
                cameraIntent.setPackage(intentpackage);
            }

            startActivityForResult(cameraIntent, 1);
atish naik
  • 226
  • 2
  • 11
0

I'm adding this comment as an update for Android 11+, because the accepted answer did not work for me before adhering to the package visibility changes.

For it to work, I've added this to the AndroidManifest.xml.

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

and, based on the accepted answer, the code to launch the camera app is:

fun resolveAndLaunchCameraApp() {
    try {
        val imageCaptureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)

        val packageManager = requireContext().packageManager
        val resolveInfo: ResolveInfo = packageManager.resolveActivity(imageCaptureIntent, 0) ?: throw Exception("Could not resolve activity!")
        val applicationInfo: ApplicationInfo =
            resolveInfo.activityInfo?.applicationInfo ?:
            resolveInfo.serviceInfo?.applicationInfo ?:
            resolveInfo.providerInfo?.applicationInfo ?: throw Exception("ApplicationInfo not found!")

        val launchIntent = packageManager.getLaunchIntentForPackage(applicationInfo.packageName) ?: throw Exception("Launch intent not found!")
        requireContext().startActivity(launchIntent)
    }
    catch (exc: Exception) {
        // handle exception
    }
}
Robert
  • 131
  • 4