问题描述
我正在尝试使用android.media.ExifInterface库从经过地理标记的图像中检索经度和纬度值.但是,尽管图像已进行地理标记,但getLatLong始终返回false.这是我的代码:
I'm trying to retrieve longitude and latitude values from geotagged images using android.media.ExifInterface library. However, getLatLong always returns false although the image is geotagged. This is my code :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button galleryButton = (Button) findViewById(R.id.galleryButton);
galleryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
ExifInterface exif = new ExInterface(selectedImage.getPath());
float[] latlong = null;
bool result = exif.getLatLong(latlong);
}
break;
}
}
如您所见,我正在用返回的URI的路径初始化exif对象,它包含诸如"/external/images/media/2330"之类的路径.当我调用getLatLong时,它总是返回false.我还尝试了此页面中的示例,但仍然不起作用.任何帮助将不胜感激.
As you can see, I'm initializing exif object with the path from returned URI and it contains path like "/external/images/media/2330". When I call getLatLong, it always returns false. I have also tried the example from this page, but still does not work. Any help would be appreciated.
Galaxy S6 EdgeAndroid SDK版本25
Galaxy S6 EdgeAndroid SDK Version 25
推荐答案
getPath()
仅适用于具有file
方案的Uri
.您有一个content
方案.另外,您正在使用ExifInterface
并存在安全漏洞.
getPath()
only works on a Uri
with a file
scheme. Yours has a content
scheme. Plus, you are using the ExifInterface
with the security flaws.
将com.android.support:exifinterface:25.3.1
库添加到您的dependencies
.
下一步,将android.media.ExifInterface
导入替换为android.support.media.ExifInterface
.
Next, replace your android.media.ExifInterface
import with one for android.support.media.ExifInterface
.
然后,替换:
ExifInterface exif = new ExInterface(selectedImage.getPath());
具有:
ExifInterface exif = new ExifInterface(getContentResolver().openInputStream(selectedImage));
这篇关于getLatLong始终返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!