Why I can't get pictureLatitude,pictureDateTime,pictureLatitude,pictureDateTime value? They all null? But I can get outputFileResults,byteArrayInputStream values.
Log.d(TAG, "filePath: $filePath") print=>filePath: /external/images/media/1000000030 . But at my real mobile phone, the photo's path is storage/emulated/0/Pictures/CameraX-Image.
====================================================================
==========================================
private fun takePhoto() {
val imageCapture2 = imageCapture1 ?: return
val name =
SimpleDateFormat(FILENAME_FORMAT, Locale.TAIWAN).format(System.currentTimeMillis())
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, name)
put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraX-Image")
}
}
val outputFileOptions = ImageCapture.OutputFileOptions.Builder(
context?.contentResolver!!,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
contentValues
).build()
imageCapture2.takePicture(
outputFileOptions,
ContextCompat.getMainExecutor(requireContext()),
object : ImageCapture.OnImageSavedCallback {
@RequiresApi(Build.VERSION_CODES.Q)
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
val msg = "Photo capture succeeded: ${outputFileResults.savedUri}"
Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show()
Log.d(TAG, "onImageSaved: $msg")
val filePath = outputFileResults.savedUri?.path ?: return
Log.d(TAG, "filePath: $filePath")
val originalUri =
MediaStore.setRequireOriginal(outputFileResults.savedUri!!)
Log.d(TAG, "originalUri: $originalUri")
val byteArrayInputStream =
outputFileResults.savedUri?.path?.byteInputStream() ?: return
Log.d(TAG, "byteArrayInputStream:$byteArrayInputStream ")
val exif = ExifInterface(byteArrayInputStream)
val pictureLatitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE)
val pictureDateTime = exif.getAttribute(ExifInterface.TAG_DATETIME)
val latLong = exif.latLong
val altitude = exif.getAltitude(0.0)
Log.d(TAG, "pictureLatitude: $pictureLatitude")
Log.d(TAG, "pictureDateTime: $pictureDateTime")
Log.d(TAG, "latLong: $latLong")
Log.d(TAG, "altitude: $altitude")
println(pictureLatitude)
}
override fun onError(exception: ImageCaptureException) {
Log.e(TAG, "Photo capture failed: ${exception.message}", exception)
}
})
}