从库中的Image访问位置数据

时间:2015-08-17 13:56:55

标签: android xamarin location xamarin.android

我有一个方法,给定一个Uri,应该从该照片中检索位置数据。但是,我得到的只是来自cursor.GetDouble(latitudeColumnIndex);方法

的零

我错过了什么?

private void GetImageLocation(Uri uri)
{
    string[] projection =
    {
        MediaStore.Images.Media.InterfaceConsts.Latitude,
        MediaStore.Images.Media.InterfaceConsts.Longitude, 

    };

    using (ICursor cursor = ContentResolver.Query(uri, projection, null, null, null))
    {
        if (cursor.MoveToFirst())
        {
            int latitudeColumnIndex = cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Latitude);
            int longitudeColumnIndex = cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Longitude);

            if (latitudeColumnIndex == -1 || longitudeColumnIndex == -1)
            {
                _newPhoto.Latitude = 0;
                _newPhoto.Longitude = 0;
            }

            _newPhoto.Latitude = cursor.GetDouble(latitudeColumnIndex);
            _newPhoto.Longitude = cursor.GetDouble(longitudeColumnIndex);
        }
        else
        {
            _newPhoto.Latitude = 0;
            _newPhoto.Longitude = 0;
        }

        cursor.Close();
    }
}

1 个答案:

答案 0 :(得分:2)

您应该尝试使用ExifInterface.GetLatLong

它接收一个浮点数组(将存储纬度和经度)并返回一个bool,指示操作是否成功。它的用法是这样的:

var exif = new ExifInterface(uri.Path);
var latLong = new float[2];
float lat, long;

//Check if Latitude and Longitude can be retrieved
if(exif.GetLatLong(latLong))
{
    lat = latLong[0];
    long = latLong[1];
}
else
{
    //Fallback
    lat = 0;
    long = 0;
}
相关问题