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 |
Tags
- 코틀린 이미지저장 #파일저장
- TwomonUSB
- 안드로이드 스튜디오 애뮬레이터
- compse state
- compse collectAsState
- livedata
- TowmonUSB 연결오류
- Room 데이터베이스 업데이트
- 데이터바인딩
- 토글 험수
- 라이브데이터 postValue
- withContext
- apk이름변경
- json 저장
- 코틀린 트리거 버튼
- mutable
Archives
- Today
- Total
EnjoyLife
이미지를 내부 저장소에 저장하는 함수 본문
fun downloadImage(url: String, folderName: String, fileName: String?) {
val appContext = applicationContext() //본인코드에 맞게 가져온다.
// 폴더 생성
val folder = File(appContext.filesDir, folderName)
if (!folder.exists() && !folder.mkdirs()) {
Log.e("FileRepository", "Failed to create directory: $folderName")
return
}
// 파일 경로 설정
val outputFile = File(folder, fileName)
if (outputFile.exists()) {
Log.e("FileRepository", "File already exists: ${outputFile.path}")
return
}
try {
val inputStream = URL(url).openStream()
val outputStream = FileOutputStream(outputFile)
inputStream.use { input ->
outputStream.use { output ->
input.copyTo(output)
}
}
Log.e("FileRepository", "Image downloaded successfully: ${outputFile.path}")
} catch (e: Exception) {
Log.e("FileRepository", "Failed to download image: ${e.message}")
}
}
'안드로이드 개발 > 개발팁' 카테고리의 다른 글
퍼센트인코딩 적용 (0) | 2024.06.21 |
---|---|
(가변)mutableList 타입으로 초기화 할때 반드시 val 변수에 담자 (0) | 2024.05.28 |
LiveData 의 변수는 언제 posVaue 와 value 을 골라서 써야될까? (0) | 2024.04.19 |
코루틴의 withContext(Dispatchers.Main) 사용하는 이유 (0) | 2024.04.17 |
초간단 리사이클러뷰 (0) | 2024.04.02 |