Рет қаралды 15,231
Hi and welcome to another tutorial from CodingDemos :)
In this tutorial, you will learn how to take pictures with the camera on Android Programmatically.
Here are the steps:
1- Open up Android Studio.
2- Initialize the Android image capture Intent inside Android button OnClickListener.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
3- Before you can call startActivityForResult(), you need to do some checking to ensure that there is an application that can handle this type of action. Otherwise, show an error Toast Message.
if(intent.resolveActivity(getPackageManager()) != null){
startActivityForResult(intent, CAMERA_ACTION_CODE);
}else{
Toast.makeText(MainActivity.this, "There is no app that support this action", Toast.LENGTH_SHORT).show();
}
4- Next you need to Override a method called onActivityResult. This method will allow you to access the data image captured by the camera.
5- You need to perform some checking first before you can access the captured image. You need to check for the requestCode, resultCode and whether the Intent data is null or not.
if(requestCode == CAMERA_ACTION_CODE && resultCode == RESULT_OK && data != null){
}
6- Inside that condition, you will retrieve the captured image data using a Bundle.
Bundle bundle = data.getExtras();
7- To be able to use the Bundle image value inside Android ImageView, you will need to use Android Bitmap.
Bitmap finalPhoto = (Bitmap) bundle.get("data");
8- Finally, you can show the image inside the imageview like this.
imageProfile.setImageBitmap(finalPhoto);
9- Now build and run the app to see the final result.
#androiddevelopment #androidprogramming #androidtutorial #androidstudio #androidstudiotutorial #java
Links:
Android photo Intent: developer.android.com/trainin...
Website: www.codingdemos.com
FaceBook: / codingdemos
Introduction 00:00
Final output 00:15
Android Studio project introduction 01:00
Using the Android Camera Intent 03:37
Check if the device supports this Intent 05:05
Android onActivityresult 10:00
Conclusion 20:00