第一步随便找个权限框架
<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" />
,图片位置权限必须,
PermissionUtils.permission("android.permission.ACCESS_MEDIA_LOCATION")
.callback(new PermissionUtils.SimpleCallback() {
@Override
public void onGranted() {
}
@Override
public void onDenied() {
ToastUtils.showShort("onDenied");
}
})
.request();
ToastUtils.showShort("onGranted");
Album.image(MainActivity.this) // Image selection.
.multipleChoice()
.camera(true)
.columnCount(3)//3 行
.selectCount(1)
.checkedList(mAlbumFiles)
.onResult(new Action<ArrayList<AlbumFile>>() {
@Override
public void onAction(@NonNull ArrayList<AlbumFile> result) {
try {
AlbumFile albumFile = result.get(0);
System.out.println(" albumFile 1: " + albumFile.getPath());
Uri uri = UriUtils.file2Uri(new File(albumFile.getPath()));
InputStream inputStream = MainActivity.this.getContentResolver().openInputStream(uri);
ExifInterface exifInterface;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
exifInterface = new ExifInterface(inputStream);
String TAG_GPS_LATITUDE = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
String TAG_GPS_LONGITUDE = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
float[] format = format(TAG_GPS_LATITUDE, TAG_GPS_LONGITUDE);
System.out.println(" TAG_GPS_LATITUDE " + format[0] + " TAG_GPS_LONGITUDE" + format[1]);
} else {
exifInterface = new ExifInterface(albumFile.getPath());
float[] output = new float[2];
exifInterface.getLatLong(output);
System.out.println(" output : " + output[0] + " " + output[1]);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// try {
// Metadata metadata = ImageMetadataReader.readMetadata(new File(albumFile.getPath()));
// Collection<GpsDirectory> directoriesOfType = metadata.getDirectoriesOfType(GpsDirectory.class);
// Iterator<GpsDirectory> iterator = directoriesOfType.iterator();
// System.out.println("xxxxxxxxxxxxxxxxx");
// while (iterator.hasNext()) {
// GpsDirectory next = iterator.next();
// double latitude = next.getGeoLocation().getLatitude();
// System.out.println("latitude" + latitude);
// }
// } catch (ImageProcessingException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
}
})
.onCancel(new Action<String>() {
@Override
public void onAction(@NonNull String result) {
}
})
.start();
public float[] format(String TAG_GPS_LATITUDE, String TAG_GPS_LONGITUDE) {
TAG_GPS_LATITUDE = TAG_GPS_LATITUDE.replace("/", ",");
TAG_GPS_LONGITUDE = TAG_GPS_LONGITUDE.replace("/", ",");
String[] lat = TAG_GPS_LATITUDE.split(",");
String[] lng = TAG_GPS_LONGITUDE.split(",");
Float latD = 0f;
Float latM = 0f;
Float latS = 0f;
Float lngD = 0f;
Float lngM = 0f;
Float lngS = 0f;
if (lat.length >= 2) {
latD = Float.parseFloat(lat[0]) / Float.parseFloat(lat[1]);
}
if (lat.length >= 4) {
latM = Float.parseFloat(lat[2]) / Float.parseFloat(lat[3]);
}
if (lat.length >= 6) {
latS = Float.parseFloat(lat[4]) / Float.parseFloat(lat[5]);
}
if (lng.length >= 1) {
lngD = Float.parseFloat(lng[0]) / Float.parseFloat(lng[1]);
}
if (lng.length >= 2) {
lngM = Float.parseFloat(lng[2]) / Float.parseFloat(lng[3]);
}
if (lng.length >= 3) {
lngS = Float.parseFloat(lng[4]) / Float.parseFloat(lng[5]);
}
float latitude = latD + latM / 60 + latS / 3600;
float longitude = lngD + lngM / 60 + lngS / 3600;
return new float[]{latitude, longitude};
}
另一种转换方法
private static double convertRationalLatLonToFloat(String rationalString, String ref) {
String[] parts = rationalString.split(",");
String[] pair;
pair = parts[0].split("/");
double degrees = Double.parseDouble(pair[0].trim()) / Double.parseDouble(pair[1].trim());
pair = parts[1].split("/");
double minutes = Double.parseDouble(pair[0].trim()) / Double.parseDouble(pair[1].trim());
pair = parts[2].split("/");
double seconds = Double.parseDouble(pair[0].trim()) / Double.parseDouble(pair[1].trim());
double result = degrees + (minutes / 60.0) + (seconds / 3600.0);
if ((ref.equals("S") || ref.equals("W"))) {
return (float) -result;
}
return result;
}
double LATITUDE = convertRationalLatLonToFloat(TAG_GPS_LATITUDE, ExifInterface.TAG_GPS_LATITUDE_REF);
double LONGITUDE = convertRationalLatLonToFloat(TAG_GPS_LONGITUDE, ExifInterface.TAG_GPS_LONGITUDE_REF);
实际上获取的位置还是差好几条街,不过统计市-区可以了
|