Skip to content

Latest commit

 

History

History
89 lines (71 loc) · 3.55 KB

from-camera.md

File metadata and controls

89 lines (71 loc) · 3.55 KB

Selecting image from camera

Create a provider

The same provider is created as demonstrated here: Creating file provider authority (by package name)

Manifest permission

<uses-permission android:name="android.permission.CAMERA"/>

Full sample code

class ImageActivity : AppCompatActivity() {

    private lateinit var binding: ActivityImageBinding

                                                  // File to store image from camera
    private val imageFile by lazy {
        File(filesDir.canonicalPath + "/app_images/", "test.img").apply {
            parentFile?.mkdirs()
        }
    }

                                                  // Method to display image from file
                                                  // on ImageView
    private fun setImageViewFromFile(){
        val bitmap = BitmapFactory.decodeFile(imageFile.absolutePath)

                                                  // ---------------- STEP 5 ----------------
        binding.imageToShow.setImageBitmap(bitmap)
    }
    
                                                  // Receiver after image from camera is returned.
    private val imageCaptureLauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
            if (it.resultCode == Activity.RESULT_OK){
            
                                                  // ---------------- STEP 4 ----------------
                                                  // If successfully snapped a photo,
                                                  // display file in ImageView
                setImageViewFromFile()
            }
        }
        
                                                  // Method to create intent and launch camera.
    private fun launchCameraIntent(){
        imageFile.delete()
        val imageFileUri = getUri(imageFile)

        val imageFromGalleryIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE).apply {
            putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri)
        }
                                                  // ---------------- STEP 3 ----------------
        imageCaptureLauncher.launch(imageFromGalleryIntent)
    }
    
    
                                                  // Camera permission launcher
    private val cameraRequestLauncher =
        registerForActivityResult(ActivityResultContracts.RequestPermission()){
            if (it){
                                                  // ---------------- STEP 2 ----------------
                                                  // If permission is granted, 
                                                  // launch intent to start camera.
                launchCameraIntent()
            }
        }

                                                  // Get Uri for a File
    private fun getUri(file: File) =
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            FileProvider.getUriForFile(this, BuildConfig.CONTENT_PROVIDER_AUTHORITY, file)
        else Uri.fromFile(file)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityImageBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.browseImages.setOnClickListener {
        
                                                  // ---------------- STEP 1 ----------------
                                                  // Launch camera permission
            cameraRequestLauncher.launch(Manifest.permission.CAMERA)
        }
    }
}