Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- chrome
- 개발
- nuxt
- JavaScript
- 코딩테스트
- 오퍼월
- spring boots
- css
- 백준
- python
- 오블완
- Express
- Jenkins
- Android
- react-native
- docker
- it
- 코테
- NanoHttpd
- AWS
- React
- Next
- nginx
- kotlin
- 티스토리챌린지
- EC2
- 파이썬
- TypeScript
- toml
- 광고 id
Archives
- Today
- Total
내맘대로 개발일지
[Kotlin] 이미지 다운로드받기 - ByteArray Image Download 본문
📱테스트 환경
Samsung Galaxy Android 13 • Android 10

이미지를 저장하는 방식을 간단히 알아보도록 하자.
01.ImageDownLoad.kt 생성
if (imageData != null) {
// 외부 저장소에 바이트 배열을 파일로 저장
val fileName = "downloaded_image.jpg"
val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName)
FileOutputStream(file).use { fileOutputStream ->
fileOutputStream.write(imageData)
}
// 갤러리에 이미지 추가
val mediaScanIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
val contentUri = Uri.fromFile(file)
mediaScanIntent.data = contentUri
context.sendBroadcast(mediaScanIntent)
} else {
// 이미지 데이터가 null인 경우 처리
Log.e("error", "Image data is null.")
}
- imageData != null → Null 체크 먼저 해준다음
- File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), fileName) → 외부 디렉토리인 Pictures 에 파일을 생성해준다.
- FileOutputStream 생성 → 스트림을 사용하여 imageData 데이터 그려준다.
- Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) → 미디어 스캐닝을 위한 인텐트를 생성한다.
- Uri.fromFile(file) → 파일로부터 contentUri를 생성해준다.
- context.sendBroadcast(mediaScanIntent) → 미디어 스캔을 요청하는 브로드캐스트를 보내주면 끝난다.
'Android > Kotlin' 카테고리의 다른 글
| [Kotlin] Storage 이미지 저장, 불러오기, 삭제 (0) | 2023.12.19 |
|---|---|
| [Kotlin] UUID 란 ? UUID 생성 하는 방법 (0) | 2023.12.01 |
| [Kotlin] 애널리틱스 로그 보내기 - Analytics (4) | 2023.11.26 |
| [Kotlin] 상태바 상태 변경하기 - StateBar Changes (1) | 2023.11.25 |
| [Kotlin] Text 에 Url 연결하기 - 택배검색 url 연결하기 (1) | 2023.11.25 |