Android Studio移除图片EXIF及写入最小化EXIF数据的Java方案求助
Hey Shawn, I totally get how frustrating it is when the tools you’re relying on (like EXIFRewriter and MetadataExtractor) don’t work as expected for EXIF manipulation. Let’s walk through reliable solutions for both removing EXIF data entirely and writing minimal EXIF tags to JPG, GIF, and PNG files.
1. Remove All EXIF Data from Images
For JPG/PNG Files
The simplest way to strip EXIF is to re-encode the image using Android’s built-in Bitmap class—since Bitmap objects don’t carry EXIF metadata by default. Here’s a reusable method:
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class ExifStripper { public static void stripExif(String inputFilePath, String outputFilePath, Bitmap.CompressFormat format) throws IOException { // Load the image into a Bitmap (discards EXIF) Bitmap bitmap = BitmapFactory.decodeFile(inputFilePath); if (bitmap == null) { throw new IOException("Failed to load image file"); } FileOutputStream outputStream = new FileOutputStream(outputFilePath); // Re-compress and save without EXIF boolean saveSuccess = bitmap.compress( format, 90, // Compression quality (only affects JPG; ignored for PNG) outputStream ); // Clean up resources outputStream.flush(); outputStream.close(); bitmap.recycle(); if (!saveSuccess) { throw new IOException("Failed to save stripped image"); } } // Example usage public void example() { String inputPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/original.jpg"; String outputPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/no_exif.jpg"; try { stripExif(inputPath, outputPath, Bitmap.CompressFormat.JPEG); } catch (IOException e) { e.printStackTrace(); } } }
For GIF Files
Since Bitmap can’t handle animated GIF frames, use the androidx.exifinterface library to directly edit the file’s metadata without breaking animation:
- Add the dependency to your app-level
build.gradle:
implementation "androidx.exifinterface:exifinterface:1.3.6"
- Code to strip GIF EXIF:
import androidx.exifinterface.media.ExifInterface; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class GifExifStripper { public static void stripGifExif(String inputPath, String outputPath) throws IOException { FileInputStream inputStream = new FileInputStream(inputPath); FileOutputStream outputStream = new FileOutputStream(outputPath); // Load EXIF and clear all attributes ExifInterface exif = new ExifInterface(inputStream); exif.resetAttributes(); exif.saveAttributes(outputStream); inputStream.close(); outputStream.close(); } }
2. Write Minimal EXIF Data to Images
The androidx.exifinterface library is the most reliable choice here—it’s maintained by Google and works consistently across Android versions. Here’s how to write only the essential EXIF tags:
Dependencies
Same as above, make sure you have the exifinterface dependency added.
Code Implementation
import androidx.exifinterface.media.ExifInterface; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class MinimalExifWriter { public static void writeMinimalExif(InputStream imageInputStream, String outputFilePath) throws IOException { ExifInterface exif = new ExifInterface(imageInputStream); // Clear all existing EXIF tags first exif.resetAttributes(); // Write only the minimal tags you need (adjust these as required) exif.setAttribute(ExifInterface.TAG_ORIENTATION, String.valueOf(ExifInterface.ORIENTATION_NORMAL)); exif.setAttribute(ExifInterface.TAG_IMAGE_WIDTH, "1920"); exif.setAttribute(ExifInterface.TAG_IMAGE_LENGTH, "1080"); // Add other essential tags like TAG_DATETIME if needed // Save the modified EXIF to the output file FileOutputStream outputStream = new FileOutputStream(outputFilePath); exif.saveAttributes(outputStream); // Clean up imageInputStream.close(); outputStream.close(); } // Example usage (replace with your image input stream) public void example() { try { InputStream inputStream = getContentResolver().openInputStream(yourImageUri); String outputPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/minimal_exif.jpg"; writeMinimalExif(inputStream, outputPath); } catch (IOException e) { e.printStackTrace(); } } }
Why Your Current Tools Aren’t Working
- EXIFRewriter: This library is no longer actively maintained, and it has compatibility issues with modern Android file systems (especially Android 10+ scoped storage), leading to frequent write failures.
- MetadataExtractor: As you noted, it’s designed only for reading EXIF data—there’s no built-in functionality to write or modify tags, so it can’t help with rewriting.
内容的提问来源于stack exchange,提问作者Shawn T

