[안드로이드] 이미지 절대경로와 이미지의 gps 정보 구하기

용씨·2021년 7월 24일
0

[공경진] fillin-map

목록 보기
2/9

이미지 절대경로 구하기

코드

// 이미지 uri를 절대 경로로 바꾸고 이미지 gps return 
    fun createCopyAndReturnRealPath(uri: Uri) :String? {
        val context = applicationContext
        val contentResolver = context.contentResolver ?: return null

        // Create file path inside app's data dir
        val filePath = (context.applicationInfo.dataDir + File.separator
                + System.currentTimeMillis())
        val file = File(filePath)
        try {
            val inputStream = contentResolver.openInputStream(uri) ?: return null
            val outputStream: OutputStream = FileOutputStream(file)
            val buf = ByteArray(1024)
            var len: Int
            while (inputStream.read(buf).also { len = it } > 0) outputStream.write(buf, 0, len)
            outputStream.close()
            inputStream.close()
        } catch (e: IOException) {
            e.printStackTrace()
        /*  절대 경로를 getGps()에 넘겨주기   */
        getGps(file.getAbsolutePath())

        return file.getAbsolutePath()
    }

참고
https://stackoverflow.com/questions/57093479/get-real-path-from-uri-data-is-deprecated-in-android-q

이미지 gps 정보 구하기

코드

import android.location.Address;
import android.location.Geocoder;
import android.util.Log;

import androidx.exifinterface.media.ExifInterface;

import java.io.IOException;
import java.util.List;

public class GPS {
    private boolean valid = false;
    private Float latitude, longitude;
    Geocoder g;

    public GPS(ExifInterface exif) {
        //주소정보 호출을 위한 GPS 정보 호출 변수
        String attrLATITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
        String attrLATITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
        String attrLONGITUDE = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
        String attrLONGITUDE_REF = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);

        if ((attrLATITUDE != null) && (attrLATITUDE_REF != null) && (attrLONGITUDE != null)
                && (attrLONGITUDE_REF != null)) {
            valid = true;

            if (attrLATITUDE_REF.equals("N")) {
                latitude = convertToDegree(attrLATITUDE);
            } else {
                latitude = 0 - convertToDegree(attrLATITUDE);
            }

            if (attrLONGITUDE_REF.equals("E")) {
                longitude = convertToDegree(attrLONGITUDE);
            } else {
                longitude = 0 - convertToDegree(attrLONGITUDE);
            }
        }
    }

    private float convertToDegree(String stringDMS) {
        Float result = null;
        String [] DMS = stringDMS.split(",",3);

        String[] stringD = DMS[0].split("/",2);
        Double D0 = Double.valueOf(stringD[0]);
        Double D1 = Double.valueOf(stringD[1]);
        Double FloatD = D0/D1;

        String[] stringM = DMS[1].split("/",2);
        Double M0 = Double.valueOf(stringM[0]);
        Double M1 = Double.valueOf(stringM[1]);
        Double FloatM = M0/M1;

        String[] stringS = DMS[2].split("/",2);
        Double S0 = Double.valueOf(stringS[0]);
        Double S1 = Double.valueOf(stringS[1]);
        Double FloatS = S0/S1;

        result = (float) (FloatD + (FloatM / 60) + (FloatS / 3600));

        return result;
    }
    public Float getLatitude() {
        return latitude;
    }
    public Float getLongitude() {
        return longitude;
    }
    public String getAddress() {
        List<Address> address=null;
        try {
            address = g.getFromLocation(latitude,longitude,10);
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("test","입출력오류");
        }
        if(address!=null){
            if(address.size()==0){
                Log.d("test", "주소찾기 오류");
            }else{
                Log.d("찾은 주소",address.get(0).getAddressLine(0));
                return address.get(0).getAddressLine(0);
            }
        }
        return null;
    }

}
// 이미지 gps 구하기
// photoPath 매개변수는 gps를 구하고 싶은 이미지의 절대 경로 
private fun getGps(photoPath: String) {
    val exif = androidx.exifinterface.media.ExifInterface(photoPath)
    val lat = GPS(exif).latitude.toDouble()
    val lng = GPS(exif).longitude.toDouble()
    Log.d("latitude", lat.toString())
    Log.d("longtitude", lng.toString())
    val g = Geocoder(context)
    var address: List<Address>? = null
    try {
        address = g.getFromLocation(lat, lng, 10)
    } catch (e: IOException) {
        e.printStackTrace()
        Log.d("test", "입출력오류")
    }
    if (address != null) {
        if (address.isEmpty()) {
            Log.d("test", "주소찾기 오류")
        } else {
            Log.d("찾은 주소", address[0].getAddressLine(0).toString())
        }
    }
}

참고
https://kylblog.tistory.com/63
https://mainia.tistory.com/1218

profile
아침형 인간이 목표

0개의 댓글