所有 ExifInterface 值 returns 0 或 null

all ExifInterface value returns 0 or null

我试图获取图像的 gps 位置,但我得到的只是 null/0,我试图从不同的图像中获取其他 exif 信息,但结果仍然是 null/0

public void MarkGeoTagImage(String imagePath)
{
    try {
        ExifInterface exif = new ExifInterface(imagePath);
Toast.makeText(MainActivity.this, imagePath, Toast.LENGTH_LONG).show();
        Toast.makeText(MainActivity.this, exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE),Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

第一个toast显示图片的绝对路径,如/storage/sdcard0/etp_images/test.jpg,但第二个toast结果只显示0或null。

可能这不是一个解决方案,但我之前 ExifInterface 遇到了一些问题。原来的 class ExifInterface 有不同的错误。尝试使用此 class 的支持版本。就我而言,它解决了我的问题。 使用 Gradle 编译:com.android.support:exifinterface:25.1.0

我遇到了同样的问题,所以我还使用以下依赖项将 ExifInterface 支持库添加到我的项目中:

compile 'com.android.support:exifinterface:25.1.0'

为了得到图像的方向角,还写了如下方法:

private int getImageAngle(Uri uri, String imagePath) {
    int orientation;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        orientation = getOrientation(uri);
    } else {
        orientation = getOrientationLegacy(imagePath);
    }

    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            return 90;
        case ExifInterface.ORIENTATION_ROTATE_180:
            return 180;
        case ExifInterface.ORIENTATION_ROTATE_270:
            return 270;
    }

    return 0;
}

@TargetApi(Build.VERSION_CODES.N)
private int getOrientation(Uri uri) {
    InputStream in = null;
    ExifInterface exif = null;

    try {
        in = getContentResolver().openInputStream(uri);
        exif = new ExifInterface(in);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {
            }
        }
    }

    return getExifAttributeInt(exif);
}

private int getOrientationLegacy(String imagePath) {
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(imagePath);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return getExifAttributeInt(exif);
}

private int getExifAttributeInt(ExifInterface exif) {
    if (exif != null) {
        return exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    } else {
        return ExifInterface.ORIENTATION_NORMAL;
    }
}