EXIF Info From A Picture File In Android Programming

This sample application list all the picture files in the external storage media and when an item is clicked its EXIF information is displayed in another activity.

Algorithm:

1.) Create a new project by File-> New -> Android Project name it ExifInterfaceExample.

2.) Create and write following into res/Layout/listitem.xml:

 

<?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="wrap_content">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/displayname"
android:textSize="18dip"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"
android:layout_weight="1.0"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/size"
android:gravity="right"
android:textSize="15dip"
android:singleLine="true"
android:ellipsize="end"/>
</LinearLayout>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/path"
android:textSize="15dip"
android:singleLine="true"
android:ellipsize="middle"
android:paddingRight="10dip"
android:paddingLeft="10dip"/>

</LinearLayout>

 

 

3.) Write following into main.xml:

 

<?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">

<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"/>

</LinearLayout>

 

 

4.) Write following into AndroidManifest.xml:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exifinterfaceexample"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ExifInterfaceExample"
android:label="@string/title_activity_exif_interface_example" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.exifinterfaceexample.ExifInfoActivity"
android:label="Exif Info">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>

</manifest>

 

 

5.) Create and Write following into res/Layouts/exif.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/exifinfo"
android:padding="5dip"/>

</LinearLayout>

 

 

6.) Create and Write following into src/ExifInfoActivity.java:

 

package com.example.exifinterfaceexample;

import java.io.IOException;

import android.app.Activity;
import android.media.ExifInterface;
import android.os.Bundle;
import android.widget.TextView;

public class ExifInfoActivity extends Activity {
public static final String FILE_PATH_KEY = "file_path";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exif);

Bundle bundle = getIntent().getExtras();

if(null != bundle){
String filepath = bundle.getString(FILE_PATH_KEY);

try {
ExifInterface exif = new ExifInterface(filepath);
StringBuilder builder = new StringBuilder();

builder.append("Date & Time: " + getExifTag(exif,ExifInterface.TAG_DATETIME) + "nn");
builder.append("Flash: " + getExifTag(exif,ExifInterface.TAG_FLASH) + "n");
builder.append("Focal Length: " + getExifTag(exif,ExifInterface.TAG_FOCAL_LENGTH) + "nn");
builder.append("GPS Datestamp: " + getExifTag(exif,ExifInterface.TAG_FLASH) + "n");
builder.append("GPS Latitude: " + getExifTag(exif,ExifInterface.TAG_GPS_LATITUDE) + "n");
builder.append("GPS Latitude Ref: " + getExifTag(exif,ExifInterface.TAG_GPS_LATITUDE_REF) + "n");
builder.append("GPS Longitude: " + getExifTag(exif,ExifInterface.TAG_GPS_LONGITUDE) + "n");
builder.append("GPS Longitude Ref: " + getExifTag(exif,ExifInterface.TAG_GPS_LONGITUDE_REF) + "n");
builder.append("GPS Processing Method: " + getExifTag(exif,ExifInterface.TAG_GPS_PROCESSING_METHOD) + "n");
builder.append("GPS Timestamp: " + getExifTag(exif,ExifInterface.TAG_GPS_TIMESTAMP) + "nn");
builder.append("Image Length: " + getExifTag(exif,ExifInterface.TAG_IMAGE_LENGTH) + "n");
builder.append("Image Width: " + getExifTag(exif,ExifInterface.TAG_IMAGE_WIDTH) + "nn");
builder.append("Camera Make: " + getExifTag(exif,ExifInterface.TAG_MAKE) + "n");
builder.append("Camera Model: " + getExifTag(exif,ExifInterface.TAG_MODEL) + "n");
builder.append("Camera Orientation: " + getExifTag(exif,ExifInterface.TAG_ORIENTATION) + "n");
builder.append("Camera White Balance: " + getExifTag(exif,ExifInterface.TAG_WHITE_BALANCE) + "n");

TextView info = (TextView)findViewById(R.id.exifinfo);

info.setText(builder.toString());

builder = null;
} catch (IOException e) {
e.printStackTrace();
}

setTitle(filepath);
}
}

private String getExifTag(ExifInterface exif,String tag){
String attribute = exif.getAttribute(tag);

return (null != attribute ? attribute : "");
}

private void setTitle(String filepath){
int pos = filepath.lastIndexOf("/");
String title = filepath;

if(-1 != pos)
{
title = filepath.substring(pos + 1);
}

super.setTitle("Exif Info: " + title);
}
}

 

 

7.) Run for output.

Steps:

1.) Create a project named ExifInterfaceExample and set the information as stated in the image.

Build Target: Android 4.0
Application Name: ExifInterfaceExample
Package Name: com. example. ExifInterfaceExample
Activity Name: ExifInterfaceExample
Min SDK Version: 2.2

2.) Open ExifInterfaceExample.java file and write following code there:

 

package com.example.exifinterfaceexample;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class ExifInterfaceExample extends ListActivity {
private PictureCursorAdapter adapter = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Cursor pictures = getContentResolver().query(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,null, null, null, null);

if(null != pictures)
{
pictures.moveToFirst();

adapter = new PictureCursorAdapter(this, R.layout.listitem, pictures);

setListAdapter(adapter);
}
}

@Override
protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);

String filepath = (String) view.getTag();
Intent intent = new Intent(this, ExifInfoActivity.class);

intent.putExtra(ExifInfoActivity.FILE_PATH_KEY, filepath);

startActivity(intent);
}

private class PictureCursorAdapter extends SimpleCursorAdapter{

public PictureCursorAdapter(Context context, int layout, Cursor c) {
super(context, layout, c,
new String[] { MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.SIZE},
new int[] { R.id.displayname, R.id.path, R.id.size });
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView)view.findViewById(R.id.displayname);
TextView path = (TextView)view.findViewById(R.id.path);
TextView size = (TextView)view.findViewById(R.id.size);

title.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));

path.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)));

int sizeIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.SIZE);

size.setText(android.text.format.Formatter.formatFileSize(ExifInterfaceExample.this, cursor.getLong(sizeIndex)));

view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.listitem, parent, false);

bindView(v, context, cursor);

return v;
}
}
}

 

 

3.) Compile and build the project.

Output

Leave a Comment:

2 comments
Ranjan says November 14, 2013

protected void onListItemClick(ListView list, View view, int position, long id) {
super.onListItemClick(list, view, position, id);

String filepath = (String) view.getTag();
Intent intent = new Intent(this, ExifInfoActivity.class);

intent.putExtra(ExifInfoActivity.FILE_PATH_KEY, filepath);

startActivity(intent);
}

in this method I am getting an error on “intent.putExtra(ExifInfoActivity.FILE_PATH_KEY, filepath);”
indicating FILE_PATH_KEY cannot be resolved or is not a field
what for is this error am completely new to Android please help
thanks in advance.

Reply
Ranjan says November 14, 2013

I have changed the
intent.putExtra(ExifInfoActivity.FILE_PATH_KEY, filepath);” to

intent.putExtra(“file_path”, filepath);”
and its awesome 🙂

Thanks for the tutorial

Reply
Add Your Reply