Create provider_paths.xml
in res/xml
folder and write below code in it.
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
Declare provider in manifests/AndroidManifest.xml
as below. Put this code in tag application
.
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
You have to add some permissions like READ_EXTERNAL_STORAGE
, WRITE_EXTERNAL_STORAGE
and CAMERA
. Add this code in manifests/AndroidManifest.xml
. Put this code in tag manifest
.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
You can add a feature from camera like autofocus
. Add this code in manifests/AndroidManifest.xml
. Put this code in tag manifest
.
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" />
This is a listener button for trigger function openCamera()
btn_takephoto.setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
openCamera()
} else {
longToast("Sorry you're version android is not support, Min Android 6.0 (Marsmallow)")
}
}
You can set directory folder and filename here.
private fun openCamera() {
val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE, "New Picture")
values.put(MediaStore.Images.Media.DESCRIPTION, "From the Camera")
//camera intent
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
// set filename
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
vFilename = "FOTO_" + timeStamp + ".jpg"
// set direcory folder
val file = File("/sdcard/niabsen/", vFilename);
val image_uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri)
startActivityForResult(cameraIntent, IMAGE_CAPTURE_CODE)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
//called when user presses ALLOW or DENY from Permission Request Popup
when(requestCode){
PERMISSION_CODE -> {
if (grantResults.size > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED){
//permission from popup was granted
openCamera()
}
else{
//permission from popup was denied
toast("Permission denied")
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
//File object of camera image
val file = File("/sdcard/niabsen/", vFilename);
longToast(file.toString())
//Uri of camera image
val uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
myImageView.setImageURI(uri)
}
}
class ExampleCode : AppCompatActivity() {
private val PERMISSION_CODE = 1000
private val IMAGE_CAPTURE_CODE = 1001
var vFilename: String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.example_code)
btn_takephoto.setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
openCamera()
} else {
longToast("Sorry you're version android is not support, Min Android 6.0 (Marsmallow)")
}
}
}
private fun openCamera() {
val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE, "New Picture")
values.put(MediaStore.Images.Media.DESCRIPTION, "From the Camera")
//camera intent
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
// set filename
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
vFilename = "FOTO_" + timeStamp + ".jpg"
// set direcory folder
val file = File("/sdcard/niabsen/", vFilename);
val image_uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri)
startActivityForResult(cameraIntent, IMAGE_CAPTURE_CODE)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
//called when user presses ALLOW or DENY from Permission Request Popup
when(requestCode){
PERMISSION_CODE -> {
if (grantResults.size > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED){
//permission from popup was granted
openCamera()
} else{
//permission from popup was denied
toast("Permission denied")
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
//File object of camera image
val file = File("/sdcard/niabsen/", vFilename);
longToast(file.toString())
//Uri of camera image
val uri = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
myImageView.setImageURI(uri)
}
}
}
Dear,
setContentView(R.layout.example_code)
btn_takephoto