Tuesday, January 8, 2019

How to Capture image and show its Exif data in Android

First of all have to capture image get into the Android image view to show. and image save in SD card normal location. to get image details like capture date, LATITUDE, LONGITUDE.

here the java code is

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MytestdroidActivity extends Activity {

private static final int IMAGE_CAPTURE = 0;
private Button startBtn, upload;
private Uri imageUri;
private ImageView imageView;
private EditText txt;
private TextView tx;
private boolean valid = false;
Float Latitude, Longitude;
EditText location, uploadby, discrip;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  imageView = (ImageView) findViewById(R.id.img);
  startBtn = (Button) findViewById(R.id.startBtn);
  // tx = (TextView) findViewById(R.id.txt);

  tx = (TextView) findViewById(R.id.txt1);
  // tx.setText(toString());
  startBtn.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    startCamera();
   }
  });

  upload = (Button) findViewById(R.id.startBtn2);
  upload.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    // String filename = uristringpic;
    // "mnt/sdcard/DCIM/Camera/1313755630067.jpg";

    try {
     ExifInterface exif = new ExifInterface(
       getRealPathFromURI(imageUri));
     ShowExif(exif);
    } catch (Exception e) {
     System.out.print(e);
    }
   }
  });
 }

 public void startCamera() {
  int imgcounter = 0;

  Log.d("ANDRO_CAMERA", "Starting camera on the phone...");
  String fileName = "testphoto.jpg";
  ContentValues values = new ContentValues();
  values.put(MediaStore.Images.Media.TITLE, fileName);
  values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

  imageUri = getContentResolver().insert(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
  // intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
  startActivityForResult(intent, IMAGE_CAPTURE);
 }

 public String getRealPathFromURI(Uri contentUri) {
  String[] proj = { MediaStore.Images.Media.DATA };
  Cursor cursor = managedQuery(contentUri, proj, null, null, null);
  int column_index = cursor
    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  return cursor.getString(column_index);
 }

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

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == IMAGE_CAPTURE) {
   if (resultCode == RESULT_OK) {
    Log.d("ANDRO_CAMERA", "Picture taken!!!");
    imageView.setImageURI(imageUri);
   }
  }
 }

 private void ShowExif(ExifInterface exif) {
  String myAttribute = "Image Information \n";

  myAttribute += getTagString(ExifInterface.TAG_DATETIME, exif);
  myAttribute += getTagString(ExifInterface.TAG_IMAGE_LENGTH, exif);
  myAttribute += getTagString(ExifInterface.TAG_IMAGE_WIDTH, 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_FLASH, exif);
  tx.setText(myAttribute);

 }

 @Override
 protected void onResume() {
  try {
   ExifInterface exif = new ExifInterface(getRealPathFromURI(imageUri));
   ShowExif(exif);
  } catch (Exception e) {
   System.out.print(e);
  }
  super.onResume();
 }
}