我有一个应用程序,我正在尝试使用其中的GPS坐标保存图像(EXIF)。当我通过画廊与应用程序共享图像时,图像是按预期发送的,但是GPS坐标正在返回null,好像它们不存在一样。
在我的应用中保存图像
这是保存的代码:
...
// put gps location into image
ExifInterface exif = new ExifInterface(pictureFile.getCanonicalPath());
List<Double> latLon = getCurrentLatLon(); // this returns lat and lon doubles values
Log.d(null, "getPictureCallback lat " + latLon.get(0) + " lon " + latLon.get(1));
GPS gps = new GPS(); // class used to convert gps coordinates
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, gps.convert(latLon.get(0)));
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, gps.convert(latLon.get(0)));
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, gps.convert(latLon.get(1)));
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, gps.convert(latLon.get(1)));
exif.saveAttributes();
Log.i("getPictureCallback LATITUDE EXTRACTED", exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE));
Log.i("getPictureCallback LONGITUDE EXTRACTED", exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE));
// write to the file
FileOutputStream fos = new FileOutputStream(pictureFile);
// save the bitmap to a file compressed as a JPEG with 100% compression rate
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
上述代码的logcat:
05-10 14:55:12.161 8560-12142/com.example.fela E/JHEAD﹕ can't open '/storage/sdcard/Pictures/fela/IMG_20150510_145512.jpg'
05-10 14:55:12.161 8560-12142/com.example.fela D/﹕ getPictureCallback lat 13.003821666666665 lon 55.60497999999999
05-10 14:55:12.161 8560-12142/com.example.fela E/JHEAD﹕ can't open '/storage/sdcard/Pictures/fela/IMG_20150510_145512.jpg'
05-10 14:55:12.161 8560-12142/com.example.fela E/JHEAD﹕ Can't write back - didn't read all
05-10 14:55:12.161 8560-12142/com.example.fela I/getPictureCallback LATITUDE EXTRACTED﹕ 13/1,0/1,13757/1000,
05-10 14:55:12.161 8560-12142/com.example.fela I/getPictureCallback LONGITUDE EXTRACTED﹕ 55/1,36/1,17927/1000,
如您所见,坐标是编写的:
getPictureCallback LATITUDE EXTRACTED﹕ 13/1,0/1,13757/1000
getPictureCallback LONGITUDE EXTRACTED﹕ 55/1,36/1,17927/1000
我假定它们现在已保存在图像文件本身中。
通过画廊分享到我的应用程序
下面是我共享保存在应用程序的图像时。
Intent intent = getActivity().getIntent();
String action = intent.getAction();
String type = intent.getType();
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
Log.v(null, imageUri.getPath());
try {
ExifInterface exif = new ExifInterface(imageUri.getPath());
String lat = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
String lon = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
Log.v(null, "lat " + lat + " lon " + lon);
} catch (IOException e) {
e.printStackTrace();
}
logcat:
05-10 14:47:25.274 8560-8560/com.example.fela V/﹕ /external/images/media/44
05-10 14:47:25.274 8560-8560/com.example.fela E/JHEAD﹕ can't open '/external/images/media/44'
05-10 14:47:25.274 8560-8560/com.example.fela V/﹕ lat null lon null
意图确实让位于我的应用程序中显示的图像,因此图像文件在那里。
但是,如您所能,图像无法提取GPS坐标,它们为无效。我不知道为什么它们在保存时检查它们时它们为无效。
https://stackoverflow.com/a/21501807/1137669
这一直是一个问题,以及我如何阅读意图图像的URI路径。正确的回答是在上面的链接中。
String imagePath = getRealPathFromURI(imageUri);
ExifInterface exif = new ExifInterface(imagePath);
String lat = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
String lon = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
和使它起作用的方法:
private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(getActivity(), contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
也如Gabe Sechan
所述,必须在bitmap.compress
之后,否则将被覆盖。
在bitmap.compress中写入fileStream时,您会覆盖文件。这覆盖了标签。扭转这些操作的顺序。