Intent 를 통해 파일을 공유하는 방법에 대해 알아보겠습니다.
만약 Intent를 이용하여 전송할 파일이 한개라면 putExtra()
전송할 파일이 여러개라면 putParcelableArrayListExtra()를 사용하면 됩니다.
1. Intent 를 이용하여 파일 한 개 보내는 방법
val fileUri = Uri.fromFile(File("/path/to/single/image.jpg"))
val intent = Intent(Intent.ACTION_SEND)
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_STREAM, fileUri)
startActivity(Intent.createChooser(intent, "Send Image"))
2. Intent 를 이용하여 파일 여러개 보내는 방법
val imageUris: ArrayList<Uri> = ArrayList()
imageUris.add(Uri.fromFile(File("/path/to/image1.jpg")))
imageUris.add(Uri.fromFile(File("/path/to/image2.jpg")))
// 필요한 만큼 이미지 Uri를 추가해주세요.
val intent = Intent(Intent.ACTION_SEND_MULTIPLE)
intent.type = "image/*"
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris)
startActivity(Intent.createChooser(intent, "Send Multiple Images"))
Uri는 각자 전송하고자 하는 파일의 경로로 변경해주시면 됩니다 :)
파일 여러개를 보내려고 할땐 putExtra를 여러번 써야하나.. 고민했는데 생각보다 간단해서 더 좋네요!
'😺 Development > Kotlin' 카테고리의 다른 글
[Kotlin] for 사용 법 : 반복문 for 문 사용법 정리 (0) | 2023.07.30 |
---|---|
[Kotlin] int To string 변환 : int 형 변수를 문자열로 변경하는 다양한 방법 (0) | 2023.07.25 |