Android(Kotlin)

image cropper

----___<<<<< 2021. 6. 22. 15:42

class MainActivity : AppCompatActivity() {
private val TAG: String = "AppDebug"
private val GALLERY_REQUEST_CODE = 1234
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
update_textview.setOnClickListener {
pickFromGallery()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
GALLERY_REQUEST_CODE -> {
if (resultCode == Activity.RESULT_OK) {
data?.data?.let { uri ->
launchImageCrop(uri)
}
}
else{
Log.e(TAG, "Image selection error: Couldn't select that image from memory." )
}
}
CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE -> {
val result = CropImage.getActivityResult(data)
if (resultCode == Activity.RESULT_OK) {
setImage(result.uri)
}
else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Log.e(TAG, "Crop error: ${result.getError()}" )
}
}
}
}
private fun setImage(uri: Uri){
Glide.with(this)
.load(uri)
.into(image)
}
private fun launchImageCrop(uri: Uri){
CropImage.activity(uri)
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1920, 1080)
.setCropShape(CropImageView.CropShape.RECTANGLE) // default is rectangle
.start(this)
}
private fun pickFromGallery() {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
intent.type = "image/*"
val mimeTypes = arrayOf("image/jpeg", "image/png", "image/jpg")
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivityForResult(intent, GALLERY_REQUEST_CODE)
}
}
view raw MainActivity.kt hosted with ❤ by GitHub

https://www.youtube.com/watch?v=DBANpg2Cl7A&t=1295s

'Android(Kotlin)' 카테고리의 다른 글

뷰바인딩 데이터바인딩  (0) 2021.06.25
android listview clipboard copy  (0) 2021.06.22
Android recyclerview 재활용문제  (0) 2021.06.21
Android unit test  (0) 2021.06.21
DTO DAO Entity  (0) 2021.06.20