Android camera for beginners in 2019.
A personal story about implementing camera feature in your app…you might find it useful

This is not meant to be a tutorial for this topic. Rather the personal beginner experience about trying and succeeding. Maybe it will help you because of some small things that you might overlook. Base on questions from StackOverflow, I was not alone with problems with implementing a camera, and that is why I decided to write this text.
First of all, everything you need to implement a camera in an Android app was in the documentation. But somehow, that was not enough for me. Maybe because I am still a Beginner.
https://developer.android.com/training/camera/photobasics#java which is one great article among others that is all that you have a need for…(and this article also :).
I will explain my steps with it, and how to create a solution that works.
In Manifest file
- Declare provider
<provider
android:name="androidx.core.content.FileProvider"//this line is something that you may also must change if you use androidx support library
android:authorities="com.strba.myapplicationx.fileprovider"//this must be according to your app
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"></meta-data>//this also can be your file_name choice
</provider>Create XML resource dir, and XML file inside
- create XML android resource directory and provider_paths.xml (or a name that you entered in Manifest), and inside this...
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/com.strba.myapplicationx/files/Readings" />//Readings is name thate later you will use in createImageFile() method---- File storageDir = getExternalFilesDir ("Readings");
</paths>In your activity file from where you want to call a camera(this is java):
1) Declare global variable (my choice)
Uri photoURI;//Declared global variable Uri because, i want to use it in other part of code2) call the camera (Bold part is all you need to change)
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity (getPackageManager ()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile ();
Log.i ("File created", "fajl je kreiran");
} catch (IOException ex) {
Log.i ("File not Created", "fajl nije kreiran");
}
// Continue only if the File was successfully created
if (photoFile != null) {
photoURI = FileProvider.getUriForFile (this,
"com.strba.myapplicationx.fileprovider",//main part this must be correect according to your app
photoFile);
takePictureIntent.putExtra (MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult (takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}3) create the file (you can play with this to create the custom file name), a bold line is all that is edited by me according to a file path in the XML file
String currentPhotoPath;
private File createImageFile() throws IOException {
// Create an unique image file name
String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss").format (new Date ());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir ("Readings");
File image = File.createTempFile (
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath ();
return image;
}4) on Activity result in my way I want to initialize Imageview, and then setImageURI
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult (requestCode, resultCode, intent);
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
ImageView imageView = findViewById (R.id.imageView);
imageView.setImageURI (photoURI);
}
}this is a result
and of course, full-res images are in your app dir on internal storage

Happy codding, hope this will help you
Regards, Nenad
