Decrypted Android Support ExiFinterface Library: Usage and practical skills
Decrypted Android Support ExiFinterface Library: Usage and practical skills
The ExiFinterface class is an important class in the Android Support library that is used to process and read photos of EXIF data.This article will introduce the usage and combat skills of the ExiFinterface class to help developers better understand and apply this class library.
1. Introduce the support library
First, we need to introduce the Android Support library in the project.Add the following dependencies to the project's Build.gradle file:
dependencies {
Implementation 'com.android.support:exifinterface: <version number>'
}
Please note that replacing the `version number>` `The SUPPORT library version number you want to use.
2. Create an ExiFinterface object
To use the ExiFinterface class, we need to create an ExiFinterface object.This can be completed by passing the photo path as a constructor parameter, for example:
String photoPath = "/sdcard/photo.jpg";
ExifInterface exifInterface = new ExifInterface(photoPath);
Please make sure you have obtained the permissions of reading the photo path.
3. Read EXIF data
Once we create an ExiFinterface object, we can use it to read the EXIF data of the photo.For example, to get the time of the photo, you can use the following code:
String dateTime = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
Similarly, you can use other methods of the ExiFinterface class to read different ExiF tags, such as:::
-` Getattribute (ExiFinterface.tag_make) `: Get photos of photo manufacturers
-` Getattribute (ExiFinterface.tag_model) `: Get photo model information
-` `Getattribute (exifinterface.tag_gps_latitude)`: Latitude information of the photo
-`Getattribute (exifinterface.tag_gps_longitude)`: Obtaining the longitudinal information of the photo
Note: When using the Getattribute () method, make sure you first check whether the return value is NULL.
4. Write into EXIF data
In addition to reading the EXIF data of photos, the ExiFinterface class also allows us to write new data into the photo.For example, set the shooting time to the current time, the following code can be used:
exifInterface.setAttribute(ExifInterface.TAG_DATETIME, getCurrentDateTime());
Please note that you need to define the GetCurrenTetetable () method to get the current date and time.
Similarly, you can use other methods of the ExiFinterface class to write different ExiF tags, such as:::
-` Setattribute (exifinterface.tag_make, "MyCamera") ``: Set photo manufacturer information "MyCamera"
-` Setattribute (exifinterface.tag_model, "mymodel") `` `: Set the photo model information" MyModel "
-` Setattribute (exifinterface.tag_gps_latitude, "37.4219999") `` `` `` `
-` Setattribute (exifinterface.tag_gps_longitude, "-122.0840575") `` `` `):
5. Save and modify
After completing the modification of EXIF data, we need to save the modification and save it back to the photo.This can be completed by calling the method of calling `exifinterface.saveattributes (), for example::
exifInterface.saveAttributes();
Please note that when saving, you need to read and write disk authority appropriately.
Summarize:
This article introduces the usage and practical skills of Android Support ExifInterface Library.It provides the function of the EXIF data of processing and reading photos, and can easily write the modified data.By using the EXIFINTERFACE class, developers can effectively operate and process the EXIF information of photos.
For complete program examples and related configuration, please refer to the following code:
// Introduce the Android Support library
dependencies {
Implementation 'com.android.support:exifinterface: <version number>'
}
// Create an ExiFinterface object and read ExiF data
String photoPath = "/sdcard/photo.jpg";
ExifInterface exifInterface = new ExifInterface(photoPath);
String dateTime = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
// Write and save the modified EXIF data
exifInterface.setAttribute(ExifInterface.TAG_DATETIME, getCurrentDateTime());
exifInterface.saveAttributes();
// Custom method, obtain the current date and time
private String getCurrentDateTime() {
// ...
}
Please make appropriate modifications and adjustments according to actual needs and project configuration.Hope this article will help you use Android SUPPORT ExiFinterface class libraries!