Read Exif Tag Of Jpg Using Exifinterface(String Filename), Alongside Requesting Permissions At Function Fourth Dimension For Android 6.0 (Api Marking 23) Or Higher.

Posted on

Last postal service exhibit how to Read Exif tag of JPG using ExifInterface(String filename), alongside Target Sdk Version to API 22. As mentioned, beginning inward Android 6.0 (API grade 23), users grant permissions to apps spell the app is running, non when they install the app. It demand extra grip of Requesting Permissions at Run Time.

This illustration add together handle Requesting Permissions at Run Time, to teach inward Target Sdk Version API 25.

uses-permission of “android.permission.READ_EXTERNAL_STORAGE” is needed inward AndroidManifest.xml, refer last post.

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

MainActivity.java

package com.blogspot.android_er.androidexif;  import android.Manifest; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageManager; 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.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; 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 course of report MainActivity extends AppCompatActivity {      somebody static concluding int RQS_OPEN_IMAGE = 1;     private static concluding int RQS_READ_EXTERNAL_STORAGE = 2;      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;                         endeavor {                             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) {                      if(CheckPermission_READ_EXTERNAL_STORAGE()){                         showExif(targetUri);                     }                  }             };      private boolean CheckPermission_READ_EXTERNAL_STORAGE() {         // render true: bring permission         // render false: no permission         if (ContextCompat.checkSelfPermission(this,                 Manifest.permission.READ_EXTERNAL_STORAGE)                 != PackageManager.PERMISSION_GRANTED) {              ActivityCompat.requestPermissions(this,                     novel String[]{Manifest.permission.READ_EXTERNAL_STORAGE},                     RQS_READ_EXTERNAL_STORAGE);              render false;         }else{             render true;         }     }      void showExif(Uri photoUri){          if(photoUri != null){              String photoPath = getRealPathFromURI(photoUri);              endeavor {                 /*                 ExifInterface (String filename) added inward API grade five                  */                 ExifInterface exifInterface = novel 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 yesteryear me,     merely I forgot where I re-create it from.      */     somebody String getRealPathFromURI(Uri uri){         String filePath = "";         String wholeID = DocumentsContract.getDocumentId(uri);          // Split at colon, role minute special 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);             }         }      }      @Override     populace void onRequestPermissionsResult(             int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {         super.onRequestPermissionsResult(requestCode, permissions, grantResults);          switch (requestCode) {             instance RQS_READ_EXTERNAL_STORAGE: {                 // If asking is cancelled, the consequence arrays are empty.                 if (grantResults.length > 0                         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                     showExif(targetUri);                 } else {                     Toast.makeText(this,                             "permission denied!",                             Toast.LENGTH_SHORT).show();                 }                 return;             }         }     } }  

Remark:
When I ready the screencast, it appear non hap Xiaomi Redmi ii running Android 4.4.4. Refer to the video above.