gpt4 book ai didi

android - 格式化从 ExifInterface.TAG_DATETIME 获取的时间和日期

转载 作者:行者123 更新时间:2023-11-29 01:35:23 27 4
gpt4 key购买 nike

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

所有信息获取成功。

现在我想格式化从 ExifInterface.TAG_DATETIME 获得的日期和时间。

我该怎么做?

主 Activity .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);
}
}

最佳答案

Chirag 发布的用于将 TAG 解析为 Joda 时间对象的代码的重写版本:

    /**
* 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;
}
}

关于android - 格式化从 ExifInterface.TAG_DATETIME 获取的时间和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28502206/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com