0

My app works correctly for the back facing camera but not for front facing.

I am unsure of what changes I should do so that when a user presses a button, the front facing camera is opened?

Should I use camera 2 API for that?

Here is my code so far:

ImageButton btnpic;
private static final int CAM_REQUEST=1313;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CAM_REQUEST){
        Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        path = saveToInternalStorage(bitmap);
        btnpic.setImageBitmap(bitmap);
    }
}

class btnTakePhotoClicker implements  Button.OnClickListener{

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
        startActivityForResult(intent, CAM_REQUEST);
    }
}

private String saveToInternalStorage(Bitmap bitmapImage){
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory,"profile.jpg");

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(mypath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return directory.getAbsolutePath();
}
Troy Poulter
  • 677
  • 1
  • 8
  • 29
Parth Shah
  • 31
  • 3

0 Answers0