Read Exif tag of JPG using ExifInterface(String filename), with Requesting Permissions at Run Time for Android 6.0 (API level 23) or higher.

Last post show how to Read Exif tag of JPG using ExifInterface(String filename), with Target Sdk Version to API 22. As mentioned, beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. It need extra handle of Requesting Permissions at Run Time.

This example add handle Requesting Permissions at Run Time, to make it Target Sdk Version API 25.

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

For the layout, refer to the example in 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;

public class MainActivity extends AppCompatActivity {

private static final int RQS_OPEN_IMAGE = 1;
private static final int RQS_READ_EXTERNAL_STORAGE = 2;

Button buttonOpen;
TextView textUri;
ImageView imageView;

Uri targetUri = null;

View.OnClickListener buttonOpenOnClickListener =
new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/jpeg");
startActivityForResult(intent, RQS_OPEN_IMAGE);
}

};
View.OnClickListener textUriOnClickListener =
new View.OnClickListener(){

@Override
public void onClick(View v) {
if (targetUri != null){
Bitmap bm;
try {
bm = BitmapFactory.decodeStream(
getContentResolver()
.openInputStream(targetUri));
imageView.setImageBitmap(bm);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

};

View.OnClickListener imageOnClickListener =
new View.OnClickListener(){

@Override
public void onClick(View view) {

if(CheckPermission_READ_EXTERNAL_STORAGE()){
showExif(targetUri);
}


}
};

private boolean CheckPermission_READ_EXTERNAL_STORAGE() {
// return true: have permission
// return false: no permission
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
RQS_READ_EXTERNAL_STORAGE);

return false;
}else{
return true;
}
}


void showExif(Uri photoUri){

if(photoUri != null){

String photoPath = getRealPathFromURI(photoUri);

try {
/*
ExifInterface (String filename) added in API level 5
*/
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();


} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Something wrong:\n" + e.toString(),
Toast.LENGTH_LONG).show();
} catch (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 not coded by me,
but I forgot where I copy it from.
*/
private String getRealPathFromURI(Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);

// Split at colon, use second item in 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, new String[]{ id }, null);

int columnIndex = cursor.getColumnIndex(column[0]);

if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return 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
public void onRequestPermissionsResult(
int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode) {
case RQS_READ_EXTERNAL_STORAGE: {
// If request is cancelled, the result 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 prepare the screencast, it seem not work on Xiaomi Redmi 2 running Android 4.4.4. Refer to the video above.

Android-er