Android EXIF GPS坐标提取不正确(通过共享意图)

Android EXIF GPS坐标提取不正确(通过共享意图),android,image,gps,Android,Image,Gps,我有一个应用程序,我正试图保存一个带有gps坐标的图像(EXIF)。当我通过gallery将图像共享到我的应用程序时,图像会按预期发送,但gps坐标返回空值,就好像它们不存在一样 在我的应用程序中保存图像 以下是保存的代码: ... // put gps location into image ExifInterface exif = new ExifInterface(pictureFile.getCanonicalPath()); List<Double> latLon = ge

我有一个应用程序,我正试图保存一个带有gps坐标的图像(EXIF)。当我通过gallery将图像共享到我的应用程序时,图像会按预期发送,但gps坐标返回空值,就好像它们不存在一样

在我的应用程序中保存图像 以下是保存的代码:

...
// 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();
如您所见,坐标是:

getPictureCallback LATITUDE EXTRACTED﹕ 13/1,0/1,13757/1000
getPictureCallback LONGITUDE EXTRACTED﹕ 55/1,36/1,17927/1000
我假设它们现在保存在图像文件本身中

通过gallery共享到我的应用程序 下面是我共享保存到应用程序中的图像的时间

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();
}
日志:

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坐标,因此它们为空。我不知道为什么它们是空的,因为我在保存时检查了它们。

在Bitmap.compress中写入FileStream时,会覆盖该文件。这将覆盖标记。颠倒这些操作的顺序。

在Bitmap.compress中写入文件流时,会覆盖该文件。这将覆盖标记。颠倒这些操作的顺序。

在Bitmap.compress中写入文件流时,会覆盖该文件。这将覆盖标记。颠倒这些操作的顺序。

在Bitmap.compress中写入文件流时,会覆盖该文件。这将覆盖标记。颠倒这些操作的顺序。

这一直是我如何读取意图图像的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);
}
同样,正如前面提到的,它必须在位图之后。压缩,否则它将被覆盖。

这一直是我如何读取意图图像的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);
}
同样,正如前面提到的,它必须在位图之后。压缩,否则它将被覆盖。

这一直是我如何读取意图图像的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);
}
同样,正如前面提到的,它必须在位图之后。压缩,否则它将被覆盖。

这一直是我如何读取意图图像的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
所提到的,它必须在
位图之后。压缩
,否则它将被覆盖。

它帮助我找到了最终答案!它帮助我找到了最后的答案!它帮助我找到了最后的答案!它帮助我找到了最后的答案!