Reputation: 47
I need to attach picture from gallery and also get the captured time of the picture selected. Is it possible to get the time?
Upvotes: 3
Views: 2761
Reputation: 4702
Here you go. This code will open the gallery and when you select a picture it will get the real path and the date, then you can do what ever you like with those.
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == Activity.RESULT_OK)
{
if (bitmap != null) bitmap.recycle();
Uri imageUri = data.getData();
String realPath = getRealPathFromURI(imageUri);
File selectedFile = new File(realPath);
Date date = new Date(selectedFile.lastModified());
String time = new SimpleDateFormat("HH:mm:ss").format(date);
Log.i("File path", realPath);
Log.i("File time", time);
bitmap = BitmapFactory.decodeFile(realPath);
imageView.setImageBitmap(bitmap);
super.onActivityResult(requestCode, resultCode, data);
}
}
public String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Upvotes: 1
Reputation: 2596
After you select any image from gallery you will get a result in onActivityResult
, use the below code in the onActivityResult
Uri selectedImageUri = data.getData();
Where data
is the Intent
in onActivityResult
Cursor cursor = getContentResolver().query(selectedImageUri, null, null,
null, null);
cursor .moveToFirst();
int column_index_date_taken = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_TAKEN);
int column_index_date_added = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATE_ADDED);
String dateTaken = cursor.getString(column_index_date_taken);
String dateAdded = cursor.getString(column_index_date_added);
Upvotes: 1
Reputation: 5440
Take a look at the link here .There will get all what you need
And here is the required code,
ExifInterface exif = new ExifInterface(filePhoto.getPath());
String date=exif.getAttribute(ExifInterface.TAG_DATETIME);
Upvotes: 1