Read Exif Tag Of Jpg Using Exifinterface(Filedescriptor)

Posted on

android.media.ExifInterface is a degree for reading in addition to writing Exif tags inward a JPEG file or a RAW icon file.
Supported formats are: JPEG, DNG, CR2, NEF, NRW, ARW, RW2, ORF, PEF, SRW in addition to RAF.
Attribute mutation is supported for JPEG icon files.

This illustration present how to opened upwards icon of jpeg using Intent.ACTION_OPEN_DOCUMENT (added inward API bird 19), thus display the icon in addition to read the Exif tag using ExifInterface (FileDescriptor)(added inward API bird 24).

Please notice:
Because ExifInterface (FileDescriptor) is used here, thus the minute Sdk Version bring to last gear upwards API 24.
To convert the Uri render yesteryear Storage Access Framework to FileDescriptor, refer to the illustration inward the document “Open Files using Storage Access Framework“.


MainActivity.java

package com.blogspot.android_er.androidexif;  import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.ExifInterface; import android.net.Uri; import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast;  import java.io.FileDescriptor; import java.io.FileNotFoundException; import java.io.IOException;  populace degree MainActivity extends AppCompatActivity {      someone static terminal int RQS_OPEN_IMAGE = 1;      Button buttonOpen;     TextView textUri;     ImageView imageView;      Uri targetUri = null;      View.OnClickListener buttonOpenOnClickListener =             novel View.OnClickListener() {                  @Override                 populace void onClick(View v) {                     Intent intent = novel Intent();                     intent.setAction(Intent.ACTION_OPEN_DOCUMENT);                     intent.addCategory(Intent.CATEGORY_OPENABLE);                     intent.setType("image/jpeg");                     startActivityForResult(intent, RQS_OPEN_IMAGE);                 }              };     View.OnClickListener textUriOnClickListener =             novel View.OnClickListener(){                  @Override                 populace void onClick(View v) {                     if (targetUri != null){                         Bitmap bm;                         travail {                             bm = BitmapFactory.decodeStream(                                     getContentResolver()                                             .openInputStream(targetUri));                             imageView.setImageBitmap(bm);                         } select handgrip of (FileNotFoundException e) {                             // TODO Auto-generated select handgrip of block                             e.printStackTrace();                         }                     }                 }              };      View.OnClickListener imageOnClickListener =             novel View.OnClickListener(){                  @Override                 populace void onClick(View view) {                     showExif(targetUri);                 }             };      void showExif(Uri photoUri){         if(photoUri != null){              ParcelFileDescriptor parcelFileDescriptor = null;              /*             How to convert the Uri to FileDescriptor, refer to the illustration inward the document:             https://developer.android.com/guide/topics/providers/document-provider.html              */             travail {                 parcelFileDescriptor = getContentResolver().openFileDescriptor(photoUri, "r");                 FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();                  /*                 ExifInterface (FileDescriptor fileDescriptor) added inward API bird 24                  */                 ExifInterface exifInterface = novel ExifInterface(fileDescriptor);                 String exif="Exif: " + fileDescriptor.toString();                 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);                  parcelFileDescriptor.close();                  Toast.makeText(getApplicationContext(),                         exif,                         Toast.LENGTH_LONG).show();              } select handgrip of (FileNotFoundException e) {                 e.printStackTrace();                 Toast.makeText(getApplicationContext(),                         "Something wrong:\n" + e.toString(),                         Toast.LENGTH_LONG).show();             } select handgrip of (IOException e) {                 e.printStackTrace();                 Toast.makeText(getApplicationContext(),                         "Something wrong:\n" + e.toString(),                         Toast.LENGTH_LONG).show();             }              String strPhotoPath = photoUri.getPath();          }else{             Toast.makeText(getApplicationContext(),                     "photoUri == null",                     Toast.LENGTH_LONG).show();         }     };      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          buttonOpen = (Button) findViewById(R.id.opendocument);         buttonOpen.setOnClickListener(buttonOpenOnClickListener);          textUri = (TextView) findViewById(R.id.texturi);         textUri.setOnClickListener(textUriOnClickListener);          imageView = (ImageView)findViewById(R.id.image);         imageView.setOnClickListener(imageOnClickListener);     }      protected void onActivityResult(int requestCode,                                     int resultCode, Intent data) {          if (resultCode == Activity.RESULT_OK) {              Uri dataUri = data.getData();              if (requestCode == RQS_OPEN_IMAGE) {                 targetUri = dataUri;                 textUri.setText(dataUri.toString());                 imageView.setImageBitmap(null);             }         }      } }  

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidexif.MainActivity" >      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <Button         android:id="@+id/opendocument"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Open Document of Image" />      <TextView         android:id="@+id/texturi"         android:layout_width="wrap_content"         android:layout_height="wrap_content" />      <ImageView         android:id="@+id/image"         android:layout_width="fill_parent"         android:layout_height="fill_parent" />  </LinearLayout> 

Next:
– Read Exif tag of JPG using ExifInterface(InputStream)
– Read Exif tag of JPG using ExifInterface(String filename)