0

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)
                }
            })
    }
Squall Huang
  • 647
  • 9
  • 20
  • `Log.d(TAG, "filePath: $filePath")` Please tell what this prints. – blackapps Oct 18 '22 at 12:18
  • @blackapps 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. – Squall Huang Oct 19 '22 at 01:54
  • Can be. But you are using the first one for exif. A nonsense path. No wonder. – blackapps Oct 19 '22 at 04:41
  • @blackapps So what should I do now? – Squall Huang Oct 19 '22 at 05:37
  • outputFileResults.savedUri?.path?.byteInputStream() change to,: outputFileResults.savedUri?.byteInputStream()? So use the uri. Not a part/path of it – blackapps Oct 19 '22 at 05:47
  • @blackapps outputFileResults.savedUri?. with no byteInputStream() method.I use val inputStream = context?.contentResolver?.openInputStream(outputFileResults.savedUri) ?: return. val exif = ExifInterface(inputStream). But still exif.getAttribute() return null. – Squall Huang Oct 19 '22 at 06:24
  • Please adapt your code above with it as i have a hard time understanding what you do.. – blackapps Oct 19 '22 at 07:18
  • Please read: https://stackoverflow.com/questions/74118668/android-exifinterface-longitude-latitude-missing-after-android-10 – blackapps Oct 19 '22 at 07:19

0 Answers0