我需要附加图库中的图片,并获取所选图片的捕获时间。有可能得到时间吗?
答案 0 :(得分:1)
你走了。此代码将打开图库,当您选择图片时,它将获得真实的路径和日期,然后您可以随心所欲地做这些。
@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);
}
答案 1 :(得分:1)
从图库中选择任何图片后,您会在onActivityResult
中获得结果,请使用onActivityResult
Uri selectedImageUri = data.getData();
其中data
是Intent
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);
答案 2 :(得分:0)
查看链接here。这将获得您需要的所有内容
这是必需的代码,
ExifInterface exif = new ExifInterface(filePhoto.getPath());
String date=exif.getAttribute(ExifInterface.TAG_DATETIME);