Sunday, January 3, 2010

Android File Explorer with JPG's Exif info

In the last exercise, Implement a simple File Explorer in Android, if a file is clicked, the application will simple display the file name only. Here, a little bit function will be add: if it's a JPG/jpg file, its Exif attributes will be listed also. This function have been described in former article, Read Exif information in a JPEG file, ExifInterface.




To check a file's extention in Java, the following code can be used:

String filename = file.getName();
String ext = filename.substring(filename.lastIndexOf('.')+1, filename.length());

if(ext.equals("JPG")||ext.equals("jpg"))
{}


To implement the Exif function in the application, it involve modification on the method onListItemClick(), and two more methods; getExif() and getTagString().
 @Override
protected void onListItemClick(ListView l, View v, int position, long id) {

File file = new File(path.get(position));

if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
else
{
String exifAttribute = null;
String filename = file.getName();
String ext = filename.substring(filename.lastIndexOf('.')+1, filename.length());

if(ext.equals("JPG")||ext.equals("jpg"))
{
try {
ExifInterface exif = new ExifInterface(file.toString());
exifAttribute = getExif(exif);
} catch (IOException e) {
// TODO Auto-generated catch block
;
}
}

new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + filename + "]")
.setMessage(exifAttribute)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}

private String getExif(ExifInterface exif)
{
String myAttribute=null;
myAttribute += getTagString(ExifInterface.TAG_DATETIME, exif);
myAttribute += getTagString(ExifInterface.TAG_FLASH, exif);
myAttribute += getTagString(ExifInterface.TAG_GPS_LATITUDE, exif);
myAttribute += getTagString(ExifInterface.TAG_GPS_LATITUDE_REF, exif);
myAttribute += getTagString(ExifInterface.TAG_GPS_LONGITUDE, exif);
myAttribute += getTagString(ExifInterface.TAG_GPS_LONGITUDE_REF, exif);
myAttribute += getTagString(ExifInterface.TAG_IMAGE_LENGTH, exif);
myAttribute += getTagString(ExifInterface.TAG_IMAGE_WIDTH, exif);
myAttribute += getTagString(ExifInterface.TAG_MAKE, exif);
myAttribute += getTagString(ExifInterface.TAG_MODEL, exif);
myAttribute += getTagString(ExifInterface.TAG_ORIENTATION, exif);
myAttribute += getTagString(ExifInterface.TAG_WHITE_BALANCE, exif);
return myAttribute;
}

private String getTagString(String tag, ExifInterface exif)
{
return(tag + " : " + exif.getAttribute(tag) + "\n");
}



Next: Android File Explorer with JPG's Exif & Photo displayed


Download the files.



2 comments:

  1. If this works for video, this will be great!

    ReplyDelete
  2. Thanks for this, it really helped with Exifs

    ReplyDelete