Read Exif Tag Of Jpg Using Exifinterface(String Filename)

Posted on


Last posts present how to Read Exif tag of JPG using ExifInterface(FileDescriptor) together with using ExifInterface(InputStream). But both ExifInterface(FileDescriptor) and ExifInterface(InputStream) added inward API bird 24, Android 7.0. In this post, I volition present how to use ExifInterface(String filename), added inward API bird 5. Such that nosotros tin fix Min Sdk Version to API 19.

To use ExifInterface(String filename), uses-permission of “android.permission.READ_EXTERNAL_STORAGE” is needed.

Beginning inward Android 6.0 (API bird 23), users grant permissions to apps piece the app is running, non when they install the app. It shout out for extra grip of Requesting Permissions at Run Time. To teach inward simple, nosotros tin fix Target Sdk Version to API 22.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.blogspot.android_er.androidexif">      <application         android:allowBackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:roundIcon="@mipmap/ic_launcher_round"         android:supportsRtl="true"         android:theme="@style/AppTheme">         <activity android:name=".MainActivity">             <intent-filter>                 <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />             </intent-filter>         </activity>     </application>     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>  </manifest> 

MainActivity.java

package com.blogspot.android_er.androidexif;  import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.media.ExifInterface; import android.net.Uri; import android.os.Bundle; import android.provider.DocumentsContract; import android.provider.MediaStore; 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.FileNotFoundException; import java.io.IOException;  populace bird MainActivity extends AppCompatActivity {      mortal static lastly 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;                         campaign {                             bm = BitmapFactory.decodeStream(                                     getContentResolver()                                             .openInputStream(targetUri));                             imageView.setImageBitmap(bm);                         } grab (FileNotFoundException e) {                             // TODO Auto-generated grab block                             e.printStackTrace();                         }                     }                 }              };      View.OnClickListener imageOnClickListener =             novel View.OnClickListener(){                  @Override                 populace void onClick(View view) {                     showExif(targetUri);                 }             };      void showExif(Uri photoUri){         if(photoUri != null){              String photoPath = getRealPathFromURI(photoUri);              campaign {                 /*                 ExifInterface (String filename) added inward API bird five                  */                 ExifInterface exifInterface = new ExifInterface(photoPath);                  String exif="Exif: ";                 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(getApplicationContext(),                         exif,                         Toast.LENGTH_LONG).show();               } grab (FileNotFoundException e) {                 e.printStackTrace();                 Toast.makeText(getApplicationContext(),                         "Something wrong:\n" + e.toString(),                         Toast.LENGTH_LONG).show();             } grab (IOException e) {                 e.printStackTrace();                 Toast.makeText(getApplicationContext(),                         "Something wrong:\n" + e.toString(),                         Toast.LENGTH_LONG).show();             }          }else{             Toast.makeText(getApplicationContext(),                     "photoUri == null",                     Toast.LENGTH_LONG).show();         }     };      /*     This method getRealPathFromURI() is non coded past times me,     simply I forgot where I re-create it from.      */     mortal String getRealPathFromURI(Uri uri){         String filePath = "";         String wholeID = DocumentsContract.getDocumentId(uri);          // Split at colon, move instant exceptional inward the array         String id = wholeID.split(":")[1];          String[] column = { MediaStore.Images.Media.DATA };          // where id is equal to         String sel = MediaStore.Images.Media._ID + "=?";          Cursor cursor = getApplicationContext().getContentResolver()                 .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,                         column, sel, novel String[]{ id }, null);          int columnIndex = cursor.getColumnIndex(column[0]);          if (cursor.moveToFirst()) {             filePath = cursor.getString(columnIndex);         }         cursor.close();         render filePath;     }      @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);             }         }      } }  

For the layout, refer to the illustration in Read Exif tag of JPG using ExifInterface(FileDescriptor).

Next:
– Read Exif tag of JPG using ExifInterface(String filename), amongst Requesting Permissions at Run Time for Android 6.0 (API bird 23) or higher.