`
啦啦啦代码西亚
  • 浏览: 16235 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

把相片的详情列表里显示的时间与文件名和标题里指示的时间统一

阅读更多

 

  1. 在Exif这个类中添加方法以取得exif中的时间(照片详情里显示的时间)

    public static String getTime(byte[] jpegData) {
            if (jpegData == null)
                return null;
            ExifInterface exif = getExif(jpegData);
            return exif.getTagStringValue(ExifInterface.TAG_DATE_TIME);
        }
     
  2. 在FileSaver类中的PhotoOperator类中调用Exif::getTime方法取得的值经过转换赋值给mDateTaken(涌来生成文件名以及title的)
                String dateTime = Exif.getTime(mData);
                if (null != dateTime) {
                    mDateTaken = stringToLong(dateTime);
                }
    注意要加保护,因为在某些模式下的多张合成照片不是每张都能在exif中取得值;并且注意在

    mTitle = createName(mFileType, mDateTaken, mGroupIndex);
     这句之前赋值。
  3. 在PhotoOperator类里面添加方法转换字符串至long型

 

private long stringToLong (String dateTime) {
            Log.d(TAG, "Brandon, the date time string is "+ dateTime);
            // eg. cut the string from 2015:12:24 10:01:52\C0\80 to 2015:12:24 10:01:52
            dateTime = dateTime.substring(0, 19);
            SimpleDateFormat format = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
            Log.d(TAG, "Brandon, the cutted string is " + dateTime);
            try {
                Date date = format.parse(dateTime);
                return date.getTime();
            } catch(ParseException e) {
                Log.e(TAG, "[stringToLong] ParseException: " + e);
                return 0;
            }
        }

 其中那个时间格式是从android相片的exif中取得的,要用substring截取第0个index到第19(不包括19)个。

注意Date的parse方法必须放在try...catch对里。

需要

import java.text.SimpleDateFormat;
import java.text.ParseException;

 

 

1
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics