ExifInterface 이미지 위치정보 가져오기
안녕하세요. 카메라앱 설정에서 위치 태그를 설정해두면 사진 촬영시 위치 정보가 같이 저장되는데
이미지의 meta 정보중에서 위치 gps 값을 가져오는 방법을 소개합니다.
https://developer.android.com/reference/androidx/exifinterface/media/ExifInterface
ExifInterface | Android Developers
ExifInterface public class ExifInterface extends Object java.lang.Object ↳ androidx.exifinterface.media.ExifInterface This is a class for reading and writing Exif tags in various image file formats. Supported for reading: JPEG, PNG, WebP, HEIF, DNG
developer.android.com
먼저 import 할때 두가지가 나오는데
import android.media.ExifInterface
import androidx.exifinterface.media.ExifInterface
아래것으로 import를 해주시면 됩니다.
import androidx.exifinterface.media.ExifInterface
그리고 이미지의 Uri 값을 가지고 있다면 실제 파일의 경로로 변환해 줍니다.
fun uri2path(context: Context, contentUri: Uri): String? {
val proj = arrayOf(MediaStore.Images.Media.DATA)
val cursor: Cursor? = context.contentResolver.query(contentUri, proj, null, null, null)
cursor?.moveToNext()
val path = cursor?.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA))
val uri = Uri.fromFile(File(path))
cursor?.close()
return path
}
그리고 중요!
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
Manifest에 선언해 주시고 런타임시에 Manifest.permission.ACCESS_MEDIA_LOCATION 을 요청해주시면 됩니다.
val exif = ExifInterface(path)
exif.latLong?.get(0) // 위도 latitude
exif.latLong?.get(1) // 경도 longitude
ExifInterface 생성자에 위에서 얻은 파일 경로를 넣어주시고,
latLong은 double array 타입이고 첫번째가 위도, 두번째가 경도값 가져오기 성공!