Sonhja
Sonhja

Reputation: 8448

How to insert Exif information to a new image?

I just create an image that comes from the camera on an Android application:

YuvImage yuv = new YuvImage(
    byteArrayDataFromCamera, 
    camera.getParameters().getPreviewFormat(),
    imageWidth, 
    imageHeight, null);

ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, imageWidth, imageHeight, 100, out);
byte[] bytes = out.toByteArray();
Bitmap image = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

And I want to add Exif information to it. Since now I've tried this:

// Save image on SD
storeImage(image, "Image_0.jpg");

String filePath = Environment.getExternalStorageDirectory() + "/MyFolder/Image_0.jpg";
try {
    ExifInterface exif = new ExifInterface(filePath);
    exif.setAttribute("UserComment", "my custom comment"); 
    exif.saveAttributes();
} 
catch (IOException e) {
    e.printStackTrace();
}

The storeImage method:

private boolean storeImage(Bitmap imageData, String filename) {
    //get path to external storage (SD card)
    String iconsStoragePath = Environment.getExternalStorageDirectory() + "/MyFolder/";
    File sdIconStorageDir = new File(iconsStoragePath);

    //create storage directories, if they don't exist
    sdIconStorageDir.mkdirs();

    try {
        String filePath = sdIconStorageDir.toString() + filename;
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);

        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

        //choose another format if PNG doesn't suit you
        imageData.compress(CompressFormat.PNG, 100, bos);

        bos.flush();
        bos.close();
    } catch (FileNotFoundException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    } catch (IOException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    }
    return true;
}

As my image is recently created, it doesn't have any ExifInterface information. I want to know how to add new ExifInterface to my recently created image. How can achieve this?

Upvotes: 0

Views: 1422

Answers (1)

I know that you can add rotation in image information but to add comment it's more complicated. The Exif class doesn't work correctly... Because you can read Exif information from stream but you can write this information. When I search information to add image info the users have the same problem as you, and I know that existing libraries to add information in image information... When I find the post I give you the URL!!


I can find the post, read this!!

Tell me if I helped you and good programming!

Upvotes: 1

Related Questions