title: How to start camera from fragment in Android
body: <p>I need a camera function in my CameraFragment.java file. I already have the code for the camera and it is working in a empty application (when I put it in my MainActivity), but I don't know where to put the code in my CameraFragment.java. </p>
  
  <p>I am really a beginner with Android Studio, but I couldn't find the answer on the internet. Also new on Stack Overflow.</p>
  
  <p>CameraFragment.java</p>
  
  <pre><code>public class CameraFragment extends Fragment{
  
  public static final String EXTRA_INFO = "default";
  
  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      return inflater.inflate(R.layout.fragment_camera, container, false);
  }
  }
  </code></pre>
  
  <p>AND I NEED THIS CODE IN MY CameraFragment FILE:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  private Button btnCapture;
  private ImageView imgCapture;
  private static final int Image_Capture_Code = 1;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.fragment_camera);
      btnCapture =(Button)findViewById(R.id.btnTakePicture);
      imgCapture = (ImageView) findViewById(R.id.capturedImage);
      btnCapture.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent cInt = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
              startActivityForResult(cInt,Image_Capture_Code);
          }
      });
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent 
      data) {
      if (requestCode == Image_Capture_Code) {
          if (resultCode == RESULT_OK) {
              Bitmap bp = (Bitmap) data.getExtras().get("data");
              imgCapture.setImageBitmap(bp);
          } else if (resultCode == RESULT_CANCELED) {
              Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
          }
      }
  }
  }
  </code></pre>