0

I have two buttons, one to select an image from the gallery and one to take a picture with your camera. The part to select it from gallery is working perfectly.

But when the user captures an image with the CameraIntent I am not able to get the URI.

Code:

  var uri_path : Uri? = null;
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == 1000) {
            println("DATA " + data?.data)

            uri_path = data?.data;

            findViewById<ImageView>(R.id.selected_image).setImageURI(data?.data);
        }

         if (resultCode == Activity.RESULT_OK && requestCode == 1001) {
            //val ImageView = findViewById<ImageView>(R.id.selected_image);
            val photo = data!!.extras!!["data"] as Bitmap?


            val values = ContentValues()
            values.put(MediaStore.Images.Media.TITLE, "Title")
            values.put(MediaStore.Images.Media.DESCRIPTION, "From Camera")
            uri_path = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)


            /*
                ATTEMPT 2:
                Error:  Caused by: java.lang.NullPointerException: MediaStore.Images.Media.…, inImage, "Title", null) must not be null
            
                uri_path = photo?.let { getImageUri(applicationContext, it) };
            */

            println("PATH " + uri_path);

            findViewById<ImageView>(R.id.selected_image).setImageBitmap(photo)
        }
    }


  fun getImageUri(inContext: Context, inImage: Bitmap): Uri? {
        val bytes = ByteArrayOutputStream()
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
        val path: String = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null)
        return Uri.parse(path)
    }

requestCode 1000 is for the gallery and works, 1001 is to take the picture from the camera and fails.

The uri_path value is NULL. The picture is displayed in the ImageView though. How can I get the image URI from the captured image?

Thanks!

O'Niel
  • 1,622
  • 1
  • 17
  • 35

0 Answers0