Search code examples
androidgalleryrational-number

How can i get exposure time by rational from photo properties in android?


I'm writing gallery.But I got double when i use exifInterface.getAttribute(ExifInterface.TAG_EXPOSURE_TIME), it should be rational(fraction). If I open system gallery, it is rational.Please help me.Thanks.


Solution

  • To get precise/correct values use the new ExifInterface support library instead of the old ExifInterface.

    You must add to your gradle:

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

    And then ensure you use the new android.support.media.ExifInterface library instead of the old android.media.ExifInterface.

    import android.support.media.ExifInterface;
    
    String getExposureTime(final ExifInterface exif)
    {
        String exposureTime = exif.getAttribute(ExifInterface.TAG_EXPOSURE_TIME);
    
        if (exposureTime != null)
        {
            exposureTime = formatExposureTime(Double.valudeOf(exposureTime));
        }
    
        return exposureTime;
    }
    
    public static String formatExposureTime(final double value)
    {
        String output;
    
        if (value < 1.0f)
        {
            output = String.format(Locale.getDefault(), "%d/%d", 1, (int)(0.5f + 1 / value));
        }
        else
        {
            final int    integer = (int)value;
            final double time    = value - integer;
            output = String.format(Locale.getDefault(), "%d''", integer);
    
            if (time > 0.0001f)
            {
                output += String.format(Locale.getDefault(), " %d/%d", 1, (int)(0.5f + 1 / time));
            }
        }
    
        return output;
    }