如何从 jpg 文件编辑元数据
How to edit metadata from jpg file
我正在尝试从 jpg 加载 Exif 元数据。我正在使用 ExifInterface class。
但是当我尝试获取一些标签时,例如ImageWidth 或 ImageLength 我得到 0 或 null。
我从 jpg 中获取元数据的方法:
String ReadExif(String file){
String exif="Exif: " + file;
try {
ExifInterface exifInterface = new ExifInterface(file);
exif += "\nIMAGE_LENGTH: " + exifInterface.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
exif += "\nIMAGE_WIDTH: " + exifInterface.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
exif += "\n DATETIME: " + exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
exif += "\n TAG_MAKE: " + exifInterface.getAttribute(ExifInterface.TAG_MAKE);
exif += "\n TAG_MODEL: " + exifInterface.getAttribute(ExifInterface.TAG_MODEL);
exif += "\n TAG_ORIENTATION: " + exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);
exif += "\n TAG_WHITE_BALANCE: " + exifInterface.getAttribute(ExifInterface.TAG_WHITE_BALANCE);
exif += "\n TAG_FOCAL_LENGTH: " + exifInterface.getAttribute(ExifInterface.TAG_FOCAL_LENGTH);
exif += "\n TAG_FLASH: " + exifInterface.getAttribute(ExifInterface.TAG_FLASH);
exif += "\nGPS related:";
exif += "\n TAG_GPS_DATESTAMP: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_DATESTAMP);
exif += "\n TAG_GPS_TIMESTAMP: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_TIMESTAMP);
exif += "\n TAG_GPS_LATITUDE: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
exif += "\n TAG_GPS_LATITUDE_REF: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
exif += "\n TAG_GPS_LONGITUDE: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
exif += "\n TAG_GPS_LONGITUDE_REF: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
exif += "\n TAG_GPS_PROCESSING_METHOD: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD);
Toast.makeText(MainActivity.this,
"finished",
Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
}
return exif;
}
我的方法调用:
textView.setText(ReadExif("/Storage/sdcard0/file.jpg"));
我不知道我做错了什么。你能帮帮我吗?
首先,您正在对路径进行硬编码。 永远不要硬编码路径。使用 Context
(例如,getExternalFilesDir()
)或 Environment
(例如,getExternalStoragePublicDirectory()
)上的方法,而不是硬编码根路径。
其次,您很可能硬编码了错误的路径,因为我还没有看到根目录中有大写字母的设备(例如,Storage
)。
我正在尝试从 jpg 加载 Exif 元数据。我正在使用 ExifInterface class。 但是当我尝试获取一些标签时,例如ImageWidth 或 ImageLength 我得到 0 或 null。
我从 jpg 中获取元数据的方法:
String ReadExif(String file){
String exif="Exif: " + file;
try {
ExifInterface exifInterface = new ExifInterface(file);
exif += "\nIMAGE_LENGTH: " + exifInterface.getAttribute(ExifInterface.TAG_IMAGE_LENGTH);
exif += "\nIMAGE_WIDTH: " + exifInterface.getAttribute(ExifInterface.TAG_IMAGE_WIDTH);
exif += "\n DATETIME: " + exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
exif += "\n TAG_MAKE: " + exifInterface.getAttribute(ExifInterface.TAG_MAKE);
exif += "\n TAG_MODEL: " + exifInterface.getAttribute(ExifInterface.TAG_MODEL);
exif += "\n TAG_ORIENTATION: " + exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);
exif += "\n TAG_WHITE_BALANCE: " + exifInterface.getAttribute(ExifInterface.TAG_WHITE_BALANCE);
exif += "\n TAG_FOCAL_LENGTH: " + exifInterface.getAttribute(ExifInterface.TAG_FOCAL_LENGTH);
exif += "\n TAG_FLASH: " + exifInterface.getAttribute(ExifInterface.TAG_FLASH);
exif += "\nGPS related:";
exif += "\n TAG_GPS_DATESTAMP: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_DATESTAMP);
exif += "\n TAG_GPS_TIMESTAMP: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_TIMESTAMP);
exif += "\n TAG_GPS_LATITUDE: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
exif += "\n TAG_GPS_LATITUDE_REF: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_LATITUDE_REF);
exif += "\n TAG_GPS_LONGITUDE: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
exif += "\n TAG_GPS_LONGITUDE_REF: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF);
exif += "\n TAG_GPS_PROCESSING_METHOD: " + exifInterface.getAttribute(ExifInterface.TAG_GPS_PROCESSING_METHOD);
Toast.makeText(MainActivity.this,
"finished",
Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(MainActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
}
return exif;
}
我的方法调用:
textView.setText(ReadExif("/Storage/sdcard0/file.jpg"));
我不知道我做错了什么。你能帮帮我吗?
首先,您正在对路径进行硬编码。 永远不要硬编码路径。使用 Context
(例如,getExternalFilesDir()
)或 Environment
(例如,getExternalStoragePublicDirectory()
)上的方法,而不是硬编码根路径。
其次,您很可能硬编码了错误的路径,因为我还没有看到根目录中有大写字母的设备(例如,Storage
)。