Simon Warta
Simon Warta

Reputation: 11408

Resize JPEG image in filesystem

Is there a way to resize a JPEG image file (from filesystem to filesystem) without removing meta data (just like ImageMagicks convert in.jpg -resize 50% out.jpg)?

The only way I found to resize .jpg files I found is BitmapFactory.decodeStream and Bitmap.compress, which looks like a lot of overhead if I manually need to transfer image meta data.

Upvotes: 10

Views: 1161

Answers (1)

Manuel Allenspach
Manuel Allenspach

Reputation: 12735

It may be possible to read the metadata with android.media.ExifInterface, compress the image via Bitmap.compress and then save the metadata back:

String filename = "yourfile.jpg";
// this reads all meta data
ExifInterface exif = new ExifInterface(filename);

// read and compress file
Bitmap bitmap = BitmapFactory.decodeFile(filename);
FileOutputStream fos = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();

// write meta data back
exif.saveAttributes();

I didn't test it, but looking at the source code of ExifInterface, it may work. If you don't want to rely on the implementation details, you could of course loop through all attributes and copy them.


Another solution would be to use the Android Exif Extended library. It is a small project to read and write exif data.

Upvotes: 3

Related Questions