3

我需要从图库中附加图片并获取所选图片的捕获时间。是否有可能获得时间?

4

3 回答 3

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);
}
于 2013-05-23T06:34:51.757 回答
1

看看这里的链接。那里会得到你需要的一切

这是所需的代码,

    ExifInterface exif = new ExifInterface(filePhoto.getPath());
    String date=exif.getAttribute(ExifInterface.TAG_DATETIME);
于 2013-05-23T05:52:15.813 回答
1

从图库中选择任何图像后,您将获得结果onActivityResult,请在onActivityResult

Uri selectedImageUri = data.getData();

在哪里data_IntentonActivityResult

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);
于 2013-05-23T07:18:31.237 回答