android - 从库中的图像访问位置数据

标签 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();
    }
}

最佳答案

您应该尝试使用ExifInterface.GetLatLong .

它接收一个 float 组(其中将存储纬度和经度)并返回一个 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;
}

关于android - 从库中的图像访问位置数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32052295/

相关文章:

Xamarin Forms 从 Visual Studio 在 iOS 上构建时间非常慢

java - 如何通过 onreceive() 函数发送位置(经度和纬度)?

android - 显示键盘时如何调整布局?

android - 什么时候需要在 android 小部件或应用程序中使用 singleTop launchMode?

android - Cocos2d-x onTouchMoved 在没有移动发生时调用

c# - 在 Xamarin.Forms 中绑定(bind) ToolbarItem 单击

android - 将 View 动画转换到屏幕边缘

xamarin - 如何在 Xamarin.forms 中立即获取 GPS 位置?

mongodb - 对大量数据使用 Mongo 地理空间查询

javascript - firefox 位置感知 + javascript 作用域