Skip to content

Instantly share code, notes, and snippets.

View traendy's full-sized avatar

Sönke Peters traendy

  • Hamburg, Germany
View GitHub Profile
@traendy
traendy / ImageFragment.kt
Created March 29, 2019 17:47
Android Method to create an intent with ACTION_IMAGE_CAPTURE and file path.
private fun takeAPicture() {
Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
takePictureIntent.resolveActivity(packageManager)?.also {
val photoFile: File? = try {
createImageFile(FILENAME)
} catch (ex: IOException) {
Log.e(TAG, ex.message)
null
}
photoFile?.also {
@traendy
traendy / ImageFragment.kt
Created March 29, 2019 17:52
Method to create an image file on an Android device.
private fun createImageFile(filename: String): File? {
val storageDir: File? = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
filename, /* name */
null, /* suffix can be null */
storageDir /* directory */
).apply {
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = absolutePath
}
@traendy
traendy / ImageFragment.kt
Created March 29, 2019 17:56
Override of onActivityResult for an REQUEST_IMAGE_CAPTURE intent.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
val imageBitmap: Bitmap?
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
imageBitmap = handleSamplingAndRotationBitmap(this, Uri.fromFile(File(currentPhotoPath)))
imageBitmap?.let {
imageView.setImageBitmap(it)
}
}
}
@traendy
traendy / ImageFragment.kt
Created March 29, 2019 17:58
Method to read bitmap from an uri.
fun handleSamplingAndRotationBitmap(context: Context, selectedImage: Uri): Bitmap? {
val imageStream = context.contentResolver.openInputStream(selectedImage)
val image = BitmapFactory.decodeStream(imageStream, null, BitmapFactory.Options())
imageStream?.close()
return if(image != null){
rotateImageIfRequired(image, selectedImage)
} else {
image
}
}
@traendy
traendy / ImageFragment.kt
Created March 29, 2019 18:01
Calls rotation depending on ExifInterface.
private fun rotateImageIfRequired(img: Bitmap, selectedImage: Uri): Bitmap? {
selectedImage.path?.let {
val orientation = ExifInterface(it).getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL)
return when (orientation) {
ExifInterface.ORIENTATION_ROTATE_90 -> rotateImage(img, 90f)
ExifInterface.ORIENTATION_ROTATE_180 -> rotateImage(img, 180f)
ExifInterface.ORIENTATION_ROTATE_270 -> rotateImage(img, 270f)
ExifInterface.ORIENTATION_FLIP_HORIZONTAL -> flip(img, true)
@traendy
traendy / ImageFragment.kt
Created March 29, 2019 18:02
Flips or rotates an Image.
private fun rotateImage(img: Bitmap, degree: Float): Bitmap {
val matrix = Matrix()
matrix.postRotate(degree)
return Bitmap.createBitmap(img, 0, 0, img.width, img.height, matrix, true)
}
private fun flip(bitmap: Bitmap, horizontal: Boolean): Bitmap {
val matrix = Matrix()
matrix.preScale((if (horizontal) -1f else 1f), (if (!horizontal) -1f else 1f))
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)