2

How do you add Location information in the image EXIF using CameraX API. I created my own app using https://codelabs.developers.google.com/codelabs/camerax-getting-started/#4.

I sifted through https://developer.android.com/training/camerax and https://developer.android.com/jetpack/androidx/releases/exifinterface but did not found any guides.

Kellin Strook
  • 487
  • 1
  • 5
  • 18

2 Answers2

1

If you took a picture you override onImageSaved. There you can write the exif data to your photo. I only have it in Java, but the princip is the same.

 ExifInterface exif = null;
      try {
          exif = new ExifInterface(PhotoPath);
      } catch (IOException e) {
          e.printStackTrace();
      }
 exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, "location");

The different TAGs you can look up here. How to get the location is here and if you need more information about how to store the location in exif look here.

Felix
  • 284
  • 2
  • 12
1

You can set the location with the Metadata.setLocation method


   val metadata = Metadata().apply {
            location = Location() // set the location here
        }

        val contentValues = ContentValues().apply {
            put(MediaStore.MediaColumns.DISPLAY_NAME, System.currentTimeMillis())
            put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
            put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM)
        }
        val contentUri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
        val outputOptions = OutputFileOptions.Builder(contentResolver, contentUri, contentValues)
            .setMetadata(metadata)
            .build()


        imageCapture.takePicture(outputOptions, mainExecutor, object : OnImageSavedCallback {
            override fun onImageSaved(outputFileResults: OutputFileResults) {
              Log.d(TAG, "Photo URI ${outputFileResults.savedUri}")
 
            }

            override fun onError(exception: ImageCaptureException) {
                Log.e(TAG, "Error", exception)
            }
        })

cyberman
  • 481
  • 4
  • 4