반응형
외부 저장소 중 앱 개별공간에서 공용공간으로 파일 복사하는 방법입니다.
안드로이드 버전 10 이상만 가능합니다.
recording.mp4 동영상 파일을 /Movies/video 폴더로 복사하는 방법
/storage/emulated/0/Android/data/com.charging.video/files/video/recording.mp4 -> /Movies/chargingMovie
FileInputStream channel 이용하여 복사 하는 방법 입니다.
MediaStore.Video.Media.RELATIVE_PATH 값에 붙여 넣을 폴더 경로 값을 지정합니다.
ContentValues 값 | |
MediaStore.Video.Media.DISPLAY_NAME | 파일명 (확장자 포함해서) |
MediaStore.Video.Media.RELATIVE_PATH | 상대경로 (Pictures, Movies, DCIM 폴더 경로) |
MediaStore.Video.Media.MIME_TYPE | 파일 종류 (mp4 파일이는 video/mp4, 이미지는 image/*) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/**
filePath = /storage/emulated/0/Android/data/com.charging.video/files/videos/recording.mp4
*/
val contentValues = ContentValues().apply { //붙여넣을곳 contentValues
put(MediaStore.Video.Media.DISPLAY_NAME, "recording.mp4")
put(MediaStore.Video.Media.RELATIVE_PATH, Environment.DIRECTORY_MOVIES + "/chargingMovie")
put(MediaStore.Video.Media.MIME_TYPE, "video/mp4")
}
val uri = mContext.contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues) // 공용폴더 Uri 생성
mContext.contentResolver.openFileDescriptor(uri!!, "w", null)?.let{
val inChannel = FileInputStream(filePath).channel // filePath는 복사할 대상 파일경로
val outChannel = FileOutputStream(it.fileDescriptor).channel // 붙여넣을 곳 채널생성
inChannel.transferTo(0, inChannel.size(), outChannel) // 파일 전송
inChannel.close() // 파일 전송 후 Close
outChannel.close()
it.close()
}
|
cs |
반응형
'프로그래밍' 카테고리의 다른 글
[Kotlin] 안드로이드 Handler 무한반복 하기 (0) | 2022.10.25 |
---|---|
[Kotlin] 1000자 단위 쉼표 붙이기 Extensions (0) | 2022.06.16 |
안드로이드 setSystemUiVisibility() Deprecated로 statusBarColor 글자색 못바꿀때 (0) | 2021.07.21 |
Firestore Could not deserialize object 오류 (0) | 2021.05.27 |