2

I just need to put a camera inside one of the three fragments of my main activity, like ([f]-[f]-[C]), where () is my main activity, [] is a fragment, C is the camera and f are just a fragment (they are full screen swipable). I need to create a whole camera (coding , etc) just for it or it is possible to call android native camera app with intent to an fagment?

Anderson
  • 111
  • 2
  • 8

3 Answers3

1

I need to create a whole camera (coding , etc) just for it

Yes, whether you write it yourself or use one from a library.

or it is possible to call android native camera app with intent to an fagment?

No, you cannot embed a third-party app in a fragment of your app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You can use an Intent to launch the camara and it will launch the camara default app., just be careful to detect when your "C" fragment is displayed, here: How to determine when Fragment becomes visible in ViewPager

If you dont do that, android pre-caches fragment before showing it and your intent will fire.

On your activity use:

@Override public void onResume() {
    super.onResume();
    if(viewPager.getCurrentItem() == 2){
        //Your code here. Executed when fragment is seen by user.
        // create Intent to take a picture and return control to the calling  application 
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

        // start the image capture Intent 
        startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }
 }

See for camera launch with intent options: http://developer.android.com/guide/topics/media/camera.html#intent-image

Community
  • 1
  • 1
Agustin
  • 114
  • 1
  • 5
0

If you need to take a picture, you can just use an intent to launch the system camera app. Doing so will make it a lot easier to code, but you won't be able to show a live preview, as you're actually handling control to the camera app through that intent.

Manually handling the entire camera lifecycle allows you to have control over the preview and show it real-time in your app. Also, if you need to have the live preview in your app, this is the way to go and can't be accomplished using just an Intent.

You might find the UltimateAndroidCameraGuide on GitHub very helpful for your problem, particularly the SimpleCameraIntentFragment and the NativeCameraFragment files in that repo.

lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46