반응형
갤러리에서 이미지를 받아왔을 경우 . 이미지를 리사이징 시켜줘야 할 때가 있습니다 . 그럴 경우에 사용하는 코드입니다..
갤러리에서 받아온 이미지 uri 를 fileUri 파라미터 값으로 넣어주고 자신이 원하는 사이즈로 설정을 해주면 됩니다.
Intent(ACTION_PICK).apply {
type = MediaStore.Images.Media.CONTENT_TYPE
putExtra(CROP, true)
putExtra(ASPECT_X, 1)
putExtra(ASPECT_Y, 1)
putExtra(OUTPUT_X, 512)
putExtra(OUTPUT_Y, 512)
choiceAlbumLauncher.launch(this)
}
private fun calculateInSampleSize(fileUri: Uri, reqWidth: Int, reqHeight: Int): Int {
val options = BitmapFactory.Options()
options.inJustDecodeBounds = true
try {
val inputStream = requireActivity().contentResolver.openInputStream(fileUri)
BitmapFactory.decodeStream(inputStream, null, options)
inputStream?.close()
} catch (e: Exception) {
e.printStackTrace()
}
val width = options.outWidth
val height = options.outHeight
var inSampleSize = 1
if (height > reqHeight || width > reqWidth) {
val halfHeight = height / 2
val halfWidth = width / 2
while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
inSampleSize *= 2
}
}
return inSampleSize
}
갤러리에서 사진 가져오는 법은 이 포스팅을 참고하면 될 것 같습니다.
https://jangstory.tistory.com/108
[Android Studio] 사진 가져오기, 사진 찍은 후 Crop 하기 1편 ! (Kotlin)
오늘은 Android Studio 사진 가져오기, 사진 찍은 후 Crop 기능을 라이브러리 사용하지 않고 설정하는 법에 대해 포스팅 하겠습니다. 1. Manifests 추가 (Manifests 를 추가했으면 당연히 permission 확인 후 검
jangstory.tistory.com
그 후에 bitmap 을 만들어서 사용하면 되는데 그 코드는 아래와 같습니다.
val calRatio = calculateInSampleSize( //위의 메서드를 이용한 size 를 가져와서 아래의 옵션에 넣어줍니다.
dataUri,
dpToPxSize(50),
dpToPxSize(50)
)
val option = BitmapFactory.Options()
option.inSampleSize = calRatio
try {
val inputStream =
requireActivity().contentResolver.openInputStream(dataUri)
val bitmap = BitmapFactory.decodeStream(inputStream, null, option)
inputStream?.close()
if (bitmap != null) {
binding.iv.setImageBitmap(bitmap)
}
} catch (e: Exception) {
e.printStackTrace()
}
추가 . 이미지 썸네일 만들기 입니다.
val ThumbImage = ThumbnailUtils.extractThumbnail(
BitmapFactory.decodeFile(mediaPath),
64, 64
)
반응형
'안드로이드' 카테고리의 다른 글
[Android Studio] BottomSheetDialogFragment 배경 투명하게 설정하기 (코틀린) (0) | 2023.06.16 |
---|---|
[Android Studio] Nested ScrollView 스크롤 위치에 따른 기능 (kotlin,코틀린) (0) | 2023.06.05 |
[Android Studio] Bitmap Image to File Convert. Kotlin (0) | 2023.04.25 |
[Android Studio] 사진 가져오기, 사진 찍은 후 Crop 하기 2편 ! (Kotlin) (0) | 2023.04.14 |
[Android Studio] 사진 가져오기, 사진 찍은 후 Crop 하기 1편 ! (Kotlin) (0) | 2023.04.11 |