Get data from a photo taken with Android, I want the exact time taken

2

I have an application that takes a picture with an intent and I must obtain the exact time when it is captured, the problem is that when I take the picture with intent , I take the time when I give it ok ( onActivityResult() ) to the captured photo, and if the user takes the photo and it takes a minute to press ok, it keeps the time of the ok and not of the moment that I take the photo, I am something new in android.

 //Comprobamos que la foto se a realizado
            if (requestCode == 1 && resultCode == RESULT_OK) {
                //Creamos un bitmap con la imagen recientemente
                //almacenada en la memoria
                        Bitmap bMap = BitmapFactory.decodeFile(
                        Environment.getExternalStorageDirectory() +
                                "/System32photo/" + file);
                SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
                Date myDate = new Date();
                String date = timeStampFormat.format(myDate);
                fechatomada =date.substring(0,8);
                horatomada = date.substring(8,14);

I think the photo in its properties has the data that I need but I do not know how to take it when the image is loaded in ImagenView

    
asked by Miguel Angel Materano 25.04.2017 в 22:11
source

1 answer

2

This is my solution, by using the class ExifInterface to be able to read the attributes of the photo you capture.

package com.AndroidExif;

import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ExifInterface;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidExifActivity extends Activity {

 String imagefile ="/sdcard/DCIM/Camera/myphoto.jpg";
 ImageView image;
 TextView Exif;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        image = (ImageView)findViewById(R.id.image);
        Exif = (TextView)findViewById(R.id.exif);
        ImageView image = (ImageView)findViewById(R.id.image);

        Bitmap bm = BitmapFactory.decodeFile(imagefile);
        image.setImageBitmap(bm);

        Exif.setText(ReadExif(imagefile));
    }

    String ReadExif(String file){
     String exif="Exif: " + file;
     try {
   ExifInterface exifInterface = new ExifInterface(file);

   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(AndroidExifActivity.this, 
     "finished", 
     Toast.LENGTH_LONG).show();

  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   Toast.makeText(AndroidExifActivity.this, 
     e.toString(), 
     Toast.LENGTH_LONG).show();
  }

     return exif;
    }

}

I got the code of this article:

Read Exif of a JPG file using ExifInterface .

    
answered by 26.04.2017 / 00:52
source