Take a photo with the Front Camera without using obsolete Apis

Asked

Viewed 610 times

1

I’m developing an app where when you login, you should take a photo!

I looked into it 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();
            }
        }

    }

He uses the android.hardware.Camera deprecated!

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

This action must be transparent to the User.

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

  • 2

    android.graphics.Camera or will be android.hardware.Camera2? If it is android.hardware.Camera2 remember that the app will only run in Android 5 or more.

  • So, it’s for version 4.4 ! I didn’t get to test the Camera2! As soon as possible I test and update! Thank you @ramaral

  • 2

    If it’s for 4.4, you’ll have to use it android.hardware.Camera. The fact that it is considered obsolete only means that it should not be used when it is possible to use the one that replaces it.

  • So @ramaral, the problem is that it doesn’t work! I implemented, and when I call the takePicture does not perform! Debugging, he calls, but does not call the onPictureTaken I’ll do some more tests! Thank you!

  • @ramaral don’t forget to leave a reply.

1 answer

1

Voce can use the camera via Intent , the code below runs in 4.1 and 6.0 , however in 6.0 You will have to add the permission in the run time (I haven’t had time to do it yet) but Oce can go in the App>Usergname>Permissions and add the permision there .

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);
        }
    }
}
  • But this way the user q will take the photo?

  • Yes, Window that takes the photo without the user knowing ?

  • This... When you enter the screen it automatically takes the photo without user action

  • It’s a little complicated, because there’s no way around the permission request

  • The permission is good! My doubt is just how to do it underneath

  • It was bad then , I did not know that Oce wanted to take the picture like this , I think that in this case Oce will have to use an if () Lse to check the version that is running your project

  • Eight thing you will have to do and check if you have the front camera , some Vices do not have

  • So this project will run on a specific device! Thanks for the help!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.