I have been trying to create an Android camera activity to test how it works on the emulator, but I am not sure if I am doing things right.
I have added the permission to the manifest for the deprecated camera version, focus and front camera. And I have been looking up tutorials and learning the code.
I have also tried to include a frame layout preview with some custom buttons, but I really don't know how to make the buttons layout overlay the frame.
Do I need to use fragments?
Also I should mention I have read about the new "camera2" and my interest to implement it to the same activity, but maybe that would be just too much for a simple test. What are your recommendations on this?
If your search for code, try the following sample code (I have also an answer at How to move image captured with the phone camera from one activity to an image view in another activity?, you can take a look).
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mFileUri = Uri.fromFile(getOutputMediaFile(1));
intent.putExtra(MediaStore.EXTRA_OUTPUT, mFileUri);
// start the image capture Intent
startActivityForResult(intent, 100);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (resultCode == RESULT_OK) {
if (mFileUri != null) {
// do something...
}
}
}
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "DCIM/Camera");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
File mediaFile;
if (type == 1) { // image
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
} else if (type == 2) { // video
mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
If using emulator to test, make sure camera supported like this: