- 특정 시점 액티비티로 인텐트 전송하기
- 혹은 데이터 저장 되게 하기
카메라 작동되게 하기- 현재 코딩실력 없으면 안보이게 하기
- 이미지 최대 5개 넣을 수 있게 하기
- 답변하기 intent값 받아서 집어넣기
- 다중이미지 선택가능하게
카메라 작동되게+회전막기
검색 중 이미지 라이브러리인 Glide를 활용해 이미지 경로를 이미지뷰에 띄우는 방법
@RequiresApi(Build.VERSION_CODES.N)
@SuppressLint("NonConstantResourceId", "QueryPermissionsNeeded")
fun onClick() {
binding.camerashootingCameraIv.setOnClickListener {
intent = Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
if (intent.resolveActivity(packageManager) != null) {
var imageFile: File? = null
try {
imageFile = createImageFile()
} catch (e: IOException) {
e.printStackTrace()
}
if (imageFile != null) {
val imageUri = FileProvider.getUriForFile(
applicationContext,
"com.example.mumulcom",
imageFile
)
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
startActivityForResult(intent, CAMERA) // final int CAMERA = 100;
}
}
binding.camerashootingCheckIb.visibility=View.VISIBLE
binding.camerashootingturnIb.visibility=View.VISIBLE
binding.camerashootingBnv.visibility=View.VISIBLE
}
binding.camerashootingGalleryIv.setOnClickListener {
intent = Intent(Intent.ACTION_PICK)
intent.type = MediaStore.Images.Media.CONTENT_TYPE
intent.type = "image/*"
startActivityForResult(intent, GALLERY)
binding.camerashootingCheckIb.visibility=View.VISIBLE
binding.camerashootingturnIb.visibility=View.VISIBLE
binding.camerashootingBnv.visibility=View.VISIBLE
}
binding.camerashootingCheckIb.setOnClickListener {
val intent=Intent(this, CheckCodingQuestionActivity::class.java)
intent.putExtra("path", imagePath)
finish()
startActivity(intent)
Log.d("PUT/path", imagePath)
}
}
// override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// super.onActivityResult(requestCode, resultCode, data)
// if (resultCode == RESULT_OK) { // 결과가 있을 경우
// if (requestCode == GALLERY) { // 갤러리 선택한 경우
//// 1) data의 주소 사용하는 방법
// imagePath = data?.dataString!! // "content://media/external/images/media/7215"
//
// }
// if (imagePath.length > 0) {
// Glide.with(this)
// .load(imagePath)
// .into(binding.ivPre)
// binding.ivPre.visibility=View.VISIBLE
// }
// }
// }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) { // 결과가 있을 경우
var bitmap: Bitmap? = null
when (requestCode) {
GALLERY -> {
// 1) 이미지 절대경로로 이미지 세팅하기
val cursor: Cursor? = contentResolver.query(data?.data!!, null, null, null, null)
if (cursor != null) {
cursor.moveToFirst()
val index: Int = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
imagePath = cursor.getString(index)
cursor.close()
}
// 2) InputStream 으로 이미지 세팅하기
try {
val inputStream: InputStream? = contentResolver.openInputStream(data.data!!)
bitmap = BitmapFactory.decodeStream(inputStream)
inputStream?.close()
binding.ivPre.setImageBitmap(bitmap)
} catch (e: IOException) {
e.printStackTrace()
}
}
CAMERA -> {
val options = BitmapFactory.Options()
options.inSampleSize = 2 // 이미지 축소 정도. 원 크기에서 1/inSampleSize 로 축소됨
bitmap = BitmapFactory.decodeFile(imagePath, options)
}
}
// binding.ivPre.setImageBitmap(bitmap)
Glide.with(this)
.load(imagePath)
.into(binding.ivPre);//글라이드 사용하면 이미지 회전을 막을 수 있음
}
}
@RequiresApi(Build.VERSION_CODES.N)
@SuppressLint("SimpleDateFormat")
@Throws(IOException::class)
fun createImageFile(): File? {
// 이미지 파일 생성
// SimpleDateFormat imageDate = new SimpleDateFormat("yyyyMMdd_HHmmss");
val timeStamp = imageDate.format(Date()) // 파일명 중복을 피하기 위한 "yyyyMMdd_HHmmss"꼴의 timeStamp
val fileName = "IMAGE_$timeStamp" // 이미지 파일 명
val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val file = File.createTempFile(fileName, ".jpg", storageDir) // 이미지 파일 생성
imagePath = file.absolutePath // 파일 절대경로 저장하기, String
return file
}
}
카메라 관련 모든 글은 다 본 것 같다.
이미지 회전을 막는 방법은 크게 두 가지 있는 것 같다 비트맵을 이용해 rotate함수 등을 이용하는 방법은 복잡하다.
하지만 glide를 사용하면 생각보다 쉽게 해결되는 문제이다.
갤러리에서는 코드가 간단한데 카메라에서는 만든 이미지의 파일 명을 설정해주는 등 여러 코드가 추가로 들어간다.
앨범 이미지 세팅하는 법
두 가지로 나누어 코드를 짰는데 아래방법은 이상하게 다른 폰으로 이미지가 안보인다.
그래서 기록용으로 두가 코드를 적어놓는다.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) { // 결과가 있을 경우
var bitmap: Bitmap? = null
when (requestCode) {
GALLERY -> {
if (requestCode == GALLERY) { // 갤러리 선택한 경우
// 1) data의 주소 사용하는 방법
imagePath = data?.dataString!! // "content://media/external/images/media/7215"
}
if (imagePath.length > 0) {
Glide.with(this)
.load(imagePath)
.into(binding.ivPre)
binding.ivPre.visibility=View.VISIBLE
}
}
CAMERA -> {
val options = BitmapFactory.Options()
options.inSampleSize = 2 // 이미지 축소 정도. 원 크기에서 1/inSampleSize 로 축소됨
bitmap = BitmapFactory.decodeFile(imagePath, options)
}
}
// binding.ivPre.setImageBitmap(bitmap)
Glide.with(this)
.load(imagePath)
.into(binding.ivPre);//글라이드 사용하면 이미지 회전을 막을 수 있음
Log.d("pathpath", imagePath)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) { // 결과가 있을 경우
var bitmap: Bitmap? = null
when (requestCode) {
GALLERY -> {
// 1) 이미지 절대경로로 이미지 세팅하기
val cursor: Cursor? = contentResolver.query(data?.data!!, null, null, null, null)
if (cursor != null) {
cursor.moveToFirst()
val index: Int = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
imagePath = cursor.getString(index)
cursor.close()
}
// 2) InputStream 으로 이미지 세팅하기
try {
val inputStream: InputStream? = contentResolver.openInputStream(data.data!!)
bitmap = BitmapFactory.decodeStream(inputStream)
inputStream?.close()
binding.ivPre.setImageBitmap(bitmap)
} catch (e: IOException) {
e.printStackTrace()
}
}
CAMERA -> {
val options = BitmapFactory.Options()
options.inSampleSize = 2 // 이미지 축소 정도. 원 크기에서 1/inSampleSize 로 축소됨
bitmap = BitmapFactory.decodeFile(imagePath, options)
}
}
// binding.ivPre.setImageBitmap(bitmap)
Glide.with(this)
.load(imagePath)
.into(binding.ivPre);//글라이드 사용하면 이미지 회전을 막을 수 있음
}
}
두 코드 모두 카메라 이미지는 세팅이 되는데 아래코드는 한 폰으로 갤러리 이미지 세팅이 되지 않는다.
전 액티비티로 이미지 url을 가져오지 못한다.
왜 그럴까
우선 이게 가능한다면 한 개만 삽입되던 이미지를 여러개 넣을 수 있다.
이미지 여러개 넣는 법을 우선 공부해봐야겠다.
'무물컴 앱개발기록' 카테고리의 다른 글
개발일지 27 - 3/5 (0) | 2022.03.05 |
---|---|
개발일지 26 - 3/3 (0) | 2022.03.05 |
개발일지 24 - 3/1 (0) | 2022.03.02 |
개발일지 23 - 2/28 (0) | 2022.03.02 |
개발일지 22 -2/25 (0) | 2022.02.28 |