I want my app to be able to capture photos without using another application. The code i used :
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = null;
try
{
photo = this.createTemporaryFile("picture", ".jpg");
photo.delete();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_LONG).show();
}
mImageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
But this code uses the phone's main camera app. Can anyone give me some code ?
Taking a picture directly using the
Camera
class is insanely complicated to get right.I am working on a library to simplify this, where you just add a
CameraFragment
to your app for the basic preview UI, and calltakePicture()
on it to take a picture, with various ways to configure the behavior (e.g., where the pictures get saved). However, this library is still a work in progress."Some code" is going to be thousands of lines long (for a complete implementation, including dealing with various device-specific oddities).
You are welcome to read the Android developer documentation on the subject.
Camera was deprecated in API 21, the new way is the use android.hardware.camera2.
To quickly summarize:
Context.getSystemService(String)
string[]
of device camera IDs by callingCameraManager.GetCameraIdList()
.Once the camera is opened, the callback provided in OpenCamera(...) will be called.
once you have the camera preview set, you need to do the following...
And the getOutputMediaFile function:
And you are done!!!
found it here