Taking a picture with the Front Camera without using obsolete APIs

1

I'm developing an app where when you log in, you should take a picture!

I researched and found the following example :

 public class PhotoHandler implements Camera.PictureCallback {

        private final Context context;

        public PhotoHandler(Context context) {
            this.context = context;
        }

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            File pictureFileDir = getDir();

            if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
                Log.d("DEBUG_TAG", "Can't create directory to save image.");
                Toast.makeText(context, "Can't create directory to save image.",
                        Toast.LENGTH_LONG).show();
                return;
            }


            String photoFile = "Picture_.jpg";
            String filename = pictureFileDir.getPath() + File.separator + photoFile;
            File pictureFile = new File(filename);
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();

            } catch (Exception error) {
                Toast.makeText(context, "Image could not be saved.",
                        Toast.LENGTH_LONG).show();
            }
        }

    }

It uses the android.hardware.Camera that is deprecated!

How can I perform this same action using android.graphics.Camera ?

This action must be transparent to the User.

By clicking the login button, he should choose the time and automatically take the photo!

    
asked by anonymous 02.08.2016 / 00:09

1 answer

1

You can use the camera via intent, the code below runs in 4.1 and 6.0, but in 6.0 you will have to add permission in run time (I have not had time to do it yet) but you can go to settins & app; YourAppName> Permissions and add to the permission.

public class CameraActivity extends Activity
{
    ImageView imageView;
    final int TAKE_PICTURE_CODE = 1;
    Uri outputFileUri;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.layout_camera_activity);

        imageView = (ImageView)findViewById(R.id.imageView);
    }
    /********** takePict **********/
    public void takePicture(View view)
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"myPicture.jpg");
        outputFileUri = Uri.fromFile(file);
        intent.putExtra(MediaStore.ACTION_IMAGE_CAPTURE,outputFileUri);

        startActivityForResult(intent, TAKE_PICTURE_CODE);
    }
    /********** onActivityResult ********/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if(data==null) Log.d("TAG-DATA-RESULT","****** null ******");
        else
        {
            Bitmap picture = (Bitmap)data.getExtras().get("data");
            imageView.setImageBitmap(picture);
        }
    }
}
    
12.08.2016 / 07:49