Reputation: 574
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.
Upvotes: 3
Views: 3682
Reputation: 323
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
}
}
Upvotes: 2
Reputation: 226
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);
Upvotes: 0
Reputation: 104
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()));
Upvotes: 2
Reputation: 781
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.)
Upvotes: 4