11

I'd like to launch the default camera, but want it to act like it was started from the launcher (i.e. the resulting picture should be stored by the camera app to the user's gallery, rather than being returned to my app). If I use Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);, the camera app uses the "OK? Retry?"-UI and doesn't save the picture. I'd rather not use a "direct" com.android.camera intent, because a lot of devices use custom camera apps. I've seen that the stock gallery3d-app uses an alias implementing com.android.camera/.Camera, but I'm not sure that every pre-loaded manufacturer camera app does this.

Nick
  • 3,504
  • 2
  • 39
  • 78

2 Answers2

13

I've now implemented it like this:

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
    PackageManager pm = mContext.getPackageManager();

    final ResolveInfo mInfo = pm.resolveActivity(i, 0);

    Intent intent = new Intent();
    intent.setComponent(new ComponentName(mInfo.activityInfo.packageName, mInfo.activityInfo.name));
    intent.setAction(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    startActivity(intent); 
} catch (Exception e){ 
    Log.i(TAG, "Unable to launch camera: " + e); 
}
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
Nick
  • 3,504
  • 2
  • 39
  • 78
  • 2
    Note, you can swap out `Intent.ACTION_MAIN` and replace the action with `MediaStore.ACTION_IMAGE_CAPTURE` to have the app start in photo-taking mode, or `MediaStore.ACTION_VIDEO_CAPTURE` to have the app start in video-taking mode. – pents90 Mar 17 '14 at 18:13
  • 1
    if you are doing this from a background service you will need to add intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); – Gal Rom Jan 08 '15 at 05:54
  • 2
    This answer doesn't work! It opens Contacts, Youtube and other irrelevant apps. – NecipAllef Jan 15 '16 at 02:27
13

This code will do the trick:

Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
context.startActivity(intent);
AChep
  • 787
  • 6
  • 10
  • 1
    This intent seems to be superior to MediaStore.ACTION_IMAGE_CAPTURE, because the previous opens certain camera apps in a mode which is exclusive for Photos, not allowing the user to switch to video. MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA does not seem to suffer from the same limitation. – Ricardo Freitas Apr 11 '16 at 07:18