Skip to content

Instantly share code, notes, and snippets.

@wajahatkarim3
Created May 1, 2020 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wajahatkarim3/1cadd3b98bb818d877ac545078185634 to your computer and use it in GitHub Desktop.
Save wajahatkarim3/1cadd3b98bb818d877ac545078185634 to your computer and use it in GitHub Desktop.
// TakePicture Contract Registration
private val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) {bitmap ->
bitmap?.let {
imageView.setImageBitmap(bitmap)
}
}
// Calling the takePicture contract
captureButton.setOnClickListener {
var imageUri: Uri? = null
takePicture(imageUri)
}
@oiyio
Copy link

oiyio commented May 4, 2021

@parvindersingh604
Copy link

in the update takepitcure launch require uri? how to pass as i passed it return null in that uri

@RahulSDeshpande
Copy link

RahulSDeshpande commented Sep 17, 2023

@oiyio & @parvindersingh604

Please follow this answer:

val directory = File(context.filesDir, "camera_images")

if (!directory.exists()) {
    directory.mkdirs()
}

val file = File(directory, "${Calendar.getInstance().timeInMillis}.png")

Uri uri = FileProvider . getUriForFile (this, getApplicationContext().getPackageName()+".provider", file)

ActivityResultLauncher<Uri> mGetContent = 
    registerForActivityResult (
        new ActivityResultContracts.TakePicture (),
        new ActivityResultCallback < Boolean >() {
    @Override
    public void onActivityResult(Boolean result) {
        // Use your 'uri' here
    }
})

mGetContent.launch(uri)

Sources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment