本文章在此基础上得补充 https://www.jianshu.com/p/ff6623351614
安卓Q一下获取地理位置没问题,安卓Q以上获取地理位置为null,也就是metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION)会返回null,
我们需要用video的uri设置setDataSource才能获取到
获取视频的uri
private static Uri getVideoContentUri(Context context, File videoFile) {
String filePath = videoFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Video.Media._ID}, MediaStore.Video.Media.DATA + "=? ",
new String[]{filePath}, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/video/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (videoFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DATA, filePath);
return context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}
获取视频的位置信息 后面可能会有一个/,处理的时候需要考虑一下
/**
* get video location info
*
* @return +31.2826+121.5048/
*/
public static String getVideoLocationInfo(Uri videoUri) {
MediaMetadataRetriever metadataRetriever = new MediaMetadataRetriever();
try {
metadataRetriever.setDataSource(GalleryAppImpl.globalContext,videoUri);
} catch (Exception e) {
e.printStackTrace();
return "";
}
return metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION);
}
public static float[] getVideoLocation(Uri videoUri) {
String s = getVideoLocationInfo(videoUri);
float[] location = new float[]{0f, 0f};
if (!TextUtils.isEmpty(s)) {
String locationInfoString = s.trim().replaceAll("/", "");
char[] chars = locationInfoString.toCharArray();
String latitude = "0";
String longitude = "0";
for (int i = 0; i < chars.length; i++) {
if ((chars[i] == '+' || chars[i] == '-') && i > 0) {
latitude = locationInfoString.substring(0, i);
longitude = locationInfoString.substring(i, chars.length);
break;
}
}
location[0] = Float.parseFloat(latitude);
location[1] = Float.parseFloat(longitude);
}
return location;
}
网友评论