exifinterface getlatlong在kotlin中不工作

brgchamk  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(234)

我试图从捕获的图像中获取纬度和经度,但exifinterface.getlatlong返回false
我添加了权限,但不起作用

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>
<uses-feature android:name="android.hardware.camera" android:required="true"/>

现在文件图像显示得很好,但是var haslatlong总是错误的,我如何得到纬度和经度?

@RequiresApi(Build.VERSION_CODES.N)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK){
        val bitmap: Bitmap
        val file = File(curPhotoPath)

        if(Build.VERSION.SDK_INT < 28)
        {
            val content = contentResolver.openInputStream(Uri.fromFile(file))
            val location = ExifInterface(content)
            var latlong = floatArrayOf()
            var hasLatlong = false

            hasLatlong = location.getLatLong(latlong)

            if(hasLatlong) {
                Log.e("lat", latlong[0].toString())
                Log.e("long", latlong[1].toString())
            }
        }    
        else
        {
            val decode = ImageDecoder.createSource(
                    this.contentResolver,
                    Uri.fromFile(file)
            )
            val content = contentResolver.openInputStream(Uri.fromFile(file))
            val location = ExifInterface(content)
            var latlong = floatArrayOf(2F)
            var hasLatlong = true
            hasLatlong = location.getLatLong(latlong)

            if(hasLatlong) {
                Log.e("lat", latlong[0].toString())
                Log.e("long", latlong[1].toString())
            }
        } 
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题