Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 格式化从ExiFinInterface.TAG\u DATETIME获取的时间和日期_Android_Date_Datetime_Time - Fatal编程技术网

Android 格式化从ExiFinInterface.TAG\u DATETIME获取的时间和日期

Android 格式化从ExiFinInterface.TAG\u DATETIME获取的时间和日期,android,date,datetime,time,Android,Date,Datetime,Time,我正在开发一个应用程序。我得到了时间、日期、经度和纬度 成功获取所有信息 现在我想格式化从ExifInterface.TAG\u DATETIME获取的日期和时间 我该怎么做 MainActivity.java public class DisplayImage extends Activity { ImageView displayImage; VideoView displayVideo; TextView imgInfo; DatabaseHelper db

我正在开发一个应用程序。我得到了时间、日期、经度和纬度

成功获取所有信息

现在我想格式化从
ExifInterface.TAG\u DATETIME
获取的日期和时间

我该怎么做

MainActivity.java

public class DisplayImage extends Activity {
    ImageView displayImage;
    VideoView displayVideo;
    TextView imgInfo;
    DatabaseHelper dbHelper;
    String mediaPath = null;

    private boolean valid = false;
    ExifInterface exifInterface;

    LocationManager locationManager;
    Location location;
    Geocoder geocoder;
    List<Address> addresses;
    Double Latitude, Longitude;
    String zip, city, state, country;
    String imgDate_Time, attrLATITUDE, attrLATITUDE_REF, attrLONGITUDE, attrLONGITUDE_REF;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_image);

        displayImage = (ImageView) findViewById(R.id.displayImage);
        displayVideo = (VideoView) findViewById(R.id.displayVideo);
        imgInfo = (TextView) findViewById(R.id.txtImgInfo);

        dbHelper = new DatabaseHelper(getApplicationContext());
        mediaPath = dbHelper.displayImages();
        File mediaFile = new File(mediaPath);

        String title = "";
        geocoder = new Geocoder(DisplayImage.this, Locale.getDefault());

        try {
            exifInterface = new ExifInterface(mediaPath);

            imgDate_Time = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);

            attrLATITUDE = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
            attrLATITUDE_REF = exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
            attrLONGITUDE = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
            attrLONGITUDE_REF = exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);

            if ((attrLATITUDE != null) && (attrLATITUDE_REF != null) && (attrLONGITUDE != null)
                    && (attrLONGITUDE_REF != null)) {
                valid = true;

                if (attrLATITUDE_REF.equals("N")) {
                    Latitude = convertToDegree(attrLATITUDE);
                } else {
                    Latitude = 0 - convertToDegree(attrLATITUDE);
                }

                if (attrLONGITUDE_REF.equals("E")) {
                    Longitude = convertToDegree(attrLONGITUDE);
                } else {
                    Longitude = 0 - convertToDegree(attrLONGITUDE);
                }

                addresses = geocoder.getFromLocation(Latitude, Longitude, 1);
                if (addresses != null && addresses.size() > 0) {

                    zip = addresses.get(0).getPostalCode();
                    city = addresses.get(0).getLocality();
                    state = addresses.get(0).getAdminArea();
                    country = addresses.get(0).getCountryName();
                    if (zip != null) {
                        title += zip + ",";
                    }
                    if (city != null) {
                        title += city + ",";
                    }
                    if (state != null) {
                        title += state + ",";
                    }
                    if (country != null) {
                        title += country;
                    }
                    imgInfo.setText(imgDate_Time + "   " + Latitude + "   " + Longitude + "  "
                            + title);
                } else {
                    title = "Unknown Location";
                    imgInfo.setText("Address Not Found");
                }

            } else {
                if (imgDate_Time != null) {
                    imgInfo.setText(imgDate_Time);
                } else {
                    imgInfo.setText("Media Information Not Available");
                }

            }

        } catch (IOException e) {

            e.printStackTrace();
        }

        if (mediaFile.exists()) {

            if (isImage(mediaPath)) {
                displayVideo.setVisibility(View.GONE);
                displayImage.setVisibility(View.VISIBLE);
                Bitmap myBitmap = BitmapFactory.decodeFile(mediaFile.getAbsolutePath());
                int height = (myBitmap.getHeight() * 512 / myBitmap.getWidth());
                Bitmap scale = Bitmap.createScaledBitmap(myBitmap, 512, height, true);
                displayImage.setImageBitmap(scale);
            } else {
                displayImage.setVisibility(View.GONE);
                displayVideo.setVisibility(View.VISIBLE);

                displayVideo.setVideoURI(Uri.parse(mediaFile.toString()));
                displayVideo.start();
            }
        }
    }

    private Double convertToDegree(String stringDMS) {
        Double result = null;
        String[] DMS = stringDMS.split(",", 3);

        String[] stringD = DMS[0].split("/", 2);
        Double D0 = new Double(stringD[0]);
        Double D1 = new Double(stringD[1]);
        Double FloatD = D0 / D1;

        String[] stringM = DMS[1].split("/", 2);
        Double M0 = new Double(stringM[0]);
        Double M1 = new Double(stringM[1]);
        Double FloatM = M0 / M1;

        String[] stringS = DMS[2].split("/", 2);
        Double S0 = new Double(stringS[0]);
        Double S1 = new Double(stringS[1]);
        Double FloatS = S0 / S1;

        result = new Double(FloatD + (FloatM / 60) + (FloatS / 3600));

        return result;

    }

    public boolean isValid() {
        return valid;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return (String.valueOf(Latitude) + ", " + String.valueOf(Longitude));
    }

    public int getLatitudeE6() {
        return (int) (Latitude * 1000000);
    }

    public int getLongitudeE6() {
        return (int) (Longitude * 1000000);
    }

    public static boolean isImage(String str) {
        boolean temp = false;
        String[] arr = { ".jpeg", ".jpg", ".png", ".bmp", ".gif" };
        for (int i = 0; i < arr.length; i++) {
            temp = str.endsWith(arr[i]);
            if (temp) {
                break;
            }
        }
        return temp;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.splash_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
公共类DisplayImage扩展活动{
图像视图显示图像;
视频视图显示视频;
文本视图imgInfo;
数据库助手dbHelper;
字符串mediaPath=null;
private boolean valid=false;
出口接口出口接口;
地点经理地点经理;
位置;
地理编码器;
列出地址;
双纬度,经度;
字符串拉链,城市,州,国家;
字符串imgDate_Time、attrLATITUDE、attrLATITUDE_REF、attrLATITUDE、attrLATITUDE_REF;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.display\u图像);
displayImage=(ImageView)findViewById(R.id.displayImage);
displayVideo=(VideoView)findviewbyd(R.id.displayVideo);
imgInfo=(TextView)findViewById(R.id.txtImgInfo);
dbHelper=newdatabasehelper(getApplicationContext());
mediaPath=dbHelper.displayImages();
文件mediaFile=新文件(mediaPath);
字符串标题=”;
geocoder=新的geocoder(DisplayImage.this,Locale.getDefault());
试一试{
ExiFinInterface=新的ExiFinInterface(mediaPath);
imgDate_Time=exifInterface.getAttribute(exifInterface.TAG_DATETIME);
attrLATITUDE=ExiFinInterface.getAttribute(ExiFinInterface.TAG\u GPS\u LATITUDE);
attrLATITUDE\u REF=ExiFinInterface.getAttribute(ExiFinInterface.TAG\u GPS\u LATITUDE\u REF);
AttrLength=ExiFinInterface.getAttribute(ExiFinInterface.TAG\u GPS\u经度);
attrLONGITUDE\u REF=ExiFinInterface.getAttribute(ExiFinInterface.TAG\u GPS\u LONGITUDE\u REF);
如果((attrLATITUDE!=null)&&(attrLATITUDE\u REF!=null)&&(attrLATITUDE!=null)
&&(属性经度(u REF!=null)){
有效=真;
如果(参考纬度等于(“N”)){
纬度=转换到度(纬度);
}否则{
纬度=0-转换到度(纬度);
}
如果(u参考等于(“E”)){
经度=convertToDegree(经度);
}否则{
经度=0-convertToDegree(经度);
}
地址=地理编码器.getFromLocation(纬度,经度,1);
if(addresses!=null&&addresses.size()>0){
zip=addresses.get(0.getPostalCode();
city=addresses.get(0.getLocation();
state=addresses.get(0.getAdminArea();
country=addresses.get(0.getCountryName();
if(zip!=null){
title+=zip+“,”;
}
如果(城市!=null){
头衔+=城市+“,”;
}
如果(状态!=null){
头衔+=州+“,”;
}
如果(国家/地区!=null){
头衔+=国家;
}
imgInfo.setText(imgDate_时间+“”+纬度+“”+经度+“”
+头衔);
}否则{
title=“未知位置”;
imgInfo.setText(“未找到地址”);
}
}否则{
if(imgDate_Time!=null){
imgInfo.setText(imgDate_时间);
}否则{
imgInfo.setText(“媒体信息不可用”);
}
}
}捕获(IOE异常){
e、 printStackTrace();
}
if(mediaFile.exists()){
if(isImage(mediaPath)){
displayVideo.setVisibility(View.GONE);
displayImage.setVisibility(View.VISIBLE);
位图myBitmap=BitmapFactory.decodeFile(mediaFile.getAbsolutePath());
int height=(myBitmap.getHeight()*512/myBitmap.getWidth());
位图比例=位图.createScaledBitmap(myBitmap,512,高度,true);
displayImage.setImageBitmap(比例);
}否则{
displayImage.setVisibility(View.GONE);
displayVideo.setVisibility(View.VISIBLE);
displayVideo.setVideoURI(Uri.parse(mediaFile.toString());
displayVideo.start();
}
}
}
专用双convertToDegree(字符串stringDMS){
双结果=空;
String[]DMS=stringDMS.split(“,”,3);
字符串[]stringD=DMS[0]。拆分(“/”,2);
Double D0=新的Double(stringD[0]);
Double D1=新的Double(stringD[1]);
双浮点数=D0/D1;
字符串[]stringM=DMS[1]。拆分(“/”,2);
Double M0=新的Double(stringM[0]);
Double M1=新的Double(stringM[1]);
双浮点数m=M0/M1;
String[]stringS=DMS[2]。拆分(“/”,2);
Double S0=新的Double(字符串[0]);
Double S1=新的Double(字符串[1]);
双浮点数=S0/S1;
结果=新的双精度(浮动+(浮动M/60)+(浮动/3600));
返回结果;
}
公共布尔值isValid(){
返回有效;
}
@凌驾
公共字符串toString(){
//TODO自动生成的方法存根
返回(String.valueOf(纬度)+“,”+String.valueOf(经度));
}
public int getLatitudeE6(){
返回(整数)(纬度*1000000);
}
public int getLongitudeE6(){
返回(整数)(经度*1000000);
}
公共静态布尔isImage(字符串str){
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd hh:mm:ss");
    SimpleDateFormat convertDate = new SimpleDateFormat("MMM dd, yyyy");
    SimpleDateFormat convertTime = new SimpleDateFormat("hh:mm:ss aa");

    Date d = null, d2 = null;

    try {
         d = simpleDateFormat.parse(imgDate_Time);
         d2 = simpleDateFormat.parse(imgDate_Time);
   } catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   String dateFormat = convertDate.format(d);
   String timeFormat = convertTime.format(d2);
    /**
     * Converts an ExifInterface time and date tag into a Joda time format
     *
     * @param EXIF_TAG_DATETIME
     * @return null in case of failure, the date object otherwise
     */
    public static LocalDateTime convert( String EXIF_TAG_DATETIME){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", Locale.US);

        try {
            return new LocalDateTime( simpleDateFormat.parse( EXIF_TAG_DATETIME ) );
        } catch (ParseException e) {
            Log.e(TAG, e.getMessage());
            return null;
        }
    }