0

I want to get the data in putExtra() but it returns null in it.data. I refered to other solutions but they are either quite old or they are in Java. I've check photoURI in Log.d and it does have a string in it, meaning the photoURI is not null.

        lateinit var photoURI: Uri
        photoFile?.also {
            Log.d(TAG, "Success creating file")
            photoURI = FileProvider.getUriForFile(
                requireContext(),
                "com.example.mobilee_commerceapp",
                it
            )
        }
        Log.d(TAG, "URI IS: " + photoURI.toString())

        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI)
        getCameraResult.launch(takePictureIntent)

it.data in getCameraResult = registerForActivityResult() shown below keeps returning null, printing the message "getCameraResult: error occured" in Logcat.

private val getCameraResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
        if(it.resultCode == Activity.RESULT_OK && it.data != null) {
            Log.d(TAG, "URI has :" + it.data!!.hasExtra("photoUri"))
        } else {
            Log.e(TAG, "getCameraResult: error occured")

I tried putting it into a Bundle, but it.data = null

I also tried using just .putExtra, and it.data still = null

AhChing11
  • 125
  • 1
  • 3
  • 15
  • You already have `photoUri` so you can just use this with `MediaStore.EXTRA_OUTPUT` your image file will be written at this path . Do not rely on `data`. – ADM Dec 30 '21 at 11:34
  • Indeed you will not get your uri back in onActivityResult. Instead you should self keep and remenber your uri or file path. – blackapps Dec 30 '21 at 11:35
  • @ADM do you mean I do not need to use putExtra() and I shud just use MediaStore.EXTRA_OUTPUT to get the URI? How can i do that? – AhChing11 Dec 30 '21 at 11:42
  • `takePictureIntent` is fine. to get the image you just check `if(it.resultCode == Activity.RESULT_OK)` and use the `photoURI` to load the image . no need to use `intent.data` here . your photo will be saved at path `photoUrl` which you have passed with `MediaStore.EXTRA_OUTPUT`. – ADM Dec 30 '21 at 11:45
  • @ADM ermm, how do i access the `photoUrl` i passed in `MediaStore.EXTRA_OUTPUT` ? – AhChing11 Dec 30 '21 at 11:49
  • @ADM owh! i can just save it in a.. global variable lol.. okay, i think i got it now, thank you! – AhChing11 Dec 30 '21 at 11:57
  • @ADM I have a problem, my URI has a value of "content://com.example...." and i set it using `.setImageURI()` but the image is not displayed – AhChing11 Dec 30 '21 at 12:04

0 Answers0