Oct 18, 2011

Read Exif of JPG file using ExifInterface

android.media.ExifInterface is a class for reading and writing Exif tags in a JPEG file. It can read and write various Exif Tag from and to JPG files, depends on API Level.

Here is a example with android:minSdkVersion="8", Android 2.2.

Read Exif of JPG file using ExifInterface

package com.AndroidExif;

import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidExifActivity extends Activity {

String imagefile ="/sdcard/DCIM/Camera/myphoto.jpg";
ImageView image;
TextView Exif;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView)findViewById(R.id.image);
Exif = (TextView)findViewById(R.id.exif);
ImageView image = (ImageView)findViewById(R.id.image);

Bitmap bm = BitmapFactory.decodeFile(imagefile);
image.setImageBitmap(bm);

Exif.setText(ReadExif(imagefile));
}

String ReadExif(String file){
String exif="Exif: " + file;
try {
ExifInterface exifInterface = new ExifInterface(file);

exif += "\nIMAGE_LENGTH: " + exifInterface.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
exif += "\nIMAGE_WIDTH: " + exifInterface.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
exif += "\n DATETIME: " + exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
exif += "\n TAG_MAKE: " + exifInterface.getAttribute(ExifInterface.TAG_MAKE);
exif += "\n TAG_MODEL: " + exifInterface.getAttribute(ExifInterface.TAG_MODEL);
exif += "\n TAG_ORIENTATION: " + exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);
exif += "\n TAG_WHITE_BALANCE: " + exifInterface.getAttribute(ExifInterface.TAG_WHITE_BALANCE);
exif += "\n TAG_FOCAL_LENGTH: " + exifInterface.getAttribute(ExifInterface.TAG_FOCAL_LENGTH);
exif += "\n TAG_FLASH: " + exifInterface.getAttribute(ExifInterface.TAG_FLASH);
exif += "\nGPS related:";
exif += "\n TAG_GPS_DATESTAMP: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_DATESTAMP);
exif += "\n TAG_GPS_TIMESTAMP: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_TIMESTAMP);
exif += "\n TAG_GPS_LATITUDE: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
exif += "\n TAG_GPS_LATITUDE_REF: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
exif += "\n TAG_GPS_LONGITUDE: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
exif += "\n TAG_GPS_LONGITUDE_REF: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
exif += "\n TAG_GPS_PROCESSING_METHOD: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD);

Toast.makeText(AndroidExifActivity.this,
"finished",
Toast.LENGTH_LONG).show();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(AndroidExifActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
}

return exif;
}

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ScrollView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/exif"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/image"
android:layout_width="300dp"
android:layout_height="300dp"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>



Related post: Convert GPS tag of ExifInterface to degree



2 comments:

  1. Hi,
    Thank you for your tutorial, it's very helpful.
    Still, I don't understand why the GPS related data is always null. Even after you enable geotag in the camera settings.

    ReplyDelete
  2. Hi,
    I am unable to find image orientation. it always gives orientation 0 either image is in potrait mode or landscape

    ReplyDelete

Infolinks In Text Ads