Mohammad Rajob Mohammad Rajob - 3 months ago 23x
Android Question

Android Extract EXIF metadata from byte[] data

I have a custom camera application. I need metadata of Image captured by the custom camera. I saved the byte data before decodebytearray (Constant.imageData1 = data;) and save it to a constant class with type byte and before using this byte data I converted it to string. When I going to execute it with ExifInterface and show it to log, then the application crashes.

Here is my OnPictureTaken Method:

PictureCallback mPicture = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Constant.imageData1 = data;
Log.e("Camrera", "22222222222222222");
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inDither = false;
// bfo.inJustDecodeBounds = true;
bfo.inPurgeable = true;
bfo.inTempStorage = new byte[16 * 1024];

Intent intent = new Intent(context, PreviewActivity.class);
// intent.putExtra("data", data);
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data, 0,
data.length, bfo);
Matrix matrix = new Matrix();
if (Constant.result == 180) {
if (Constant.result == 270) {
int height = bitmapPicture.getHeight();
int width = bitmapPicture.getWidth();
//Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapPicture,
//height, width, true);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapPicture, 0, 0,
bitmapPicture.getWidth(), bitmapPicture.getHeight(), matrix,
ByteArrayOutputStream blob = new ByteArrayOutputStream();
Log.e("Camrera1", "22222222222222222");
50 /* ignored for PNG */, blob);
byte[] bitmapdata = blob.toByteArray();
Constant.imageData = bitmapdata;
Log.e("Camrera2", "22222222222222222");


Here is my execution code:

private void SaveImage() {
try {
String data = byteArrayToString(Constant.imageData1);
ExifInterface ex = new ExifInterface(data);
String make = ex.getAttribute(ExifInterface.TAG_MAKE);
Log.e("Make", make);
Log.e("Make", make);
Log.e("Make", make);

} catch (Exception e) {

And the bytearraytostring method is:

public static String byteArrayToString(byte[] bytes)
return new String(bytes);

This is very important to me. Please help me.


Please try the code snippet below, and change it where you need to. Warning: I haven't tested it.

So, basically, what I did is this:

  • Step 1. Get the byte array from the camera in the onPictureTaken method.
  • Step 2. Create a file on the SDCard and write the byte array to the file
  • Step 3. Read Exif Metadata from File-Path

    public void onPictureTaken(byte[] data, Camera camera) {
        //Step 1. Create file for storing image data on SDCard
        File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File pictureFileDir = new File(sdDir, "RDCCameraImages");
        if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
          Log.d(TAG, "Can't create directory to save image.");
        //Step 2. write image byte array to file
        String photoFile = "Picture_" + date + ".jpg";
        String imageFilePath = pictureFileDir.getPath() + File.separator + photoFile;
        File pictureFile = new File(imageFilePath);
              FileOutputStream fos = new FileOutputStream(pictureFile);
              Toast.makeText(context, "New Image saved:" + photoFile,
            } catch (Exception error) {
              Log.d(TAG, "File" + filename + "not saved: "
                  + error.getMessage());
              Toast.makeText(context, "Image could not be saved.",
        //Step 3. Get Exif Info from File path
        ExifInterface exif;
        try {
            exif = new ExifInterface(imageFilePath);
            String make = exif.getAttribute(ExifInterface.TAG_MAKE);
        } catch (IOException e) {
        //check the value of  “make” here