I am creating a bottom nav program that involves two buttons. One to open up the camera and one to import pictures from the camera roll. This was working on a singular fragment but implementing the bottom nav is giving me no errors and I am crashing. Please help! I have been messing with this thing for hours no with no luck. I feel like it has somethig to do with the placement of the pre given code (bindings and such)
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.fragment_first_screen.*
class HomeFragment : Fragment() { //changed from fragment
companion object {
val IMAGE_REQUEST_CODE = 100
}
private lateinit var button: Button
private val cameraRequest = 1888
lateinit var imageView: ImageView
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val myview: View = inflater.inflate(R.layout.fragment_home, container, false)
val button = myview.findViewById<Button>(R.id.plantSelectImage)
button.setOnClickListener {
pickImageGallery()
Toast.makeText(myview.context, "HI", Toast.LENGTH_SHORT).show()
}
if (ContextCompat.checkSelfPermission(myview.context, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED
)
this.activity?.let {
ActivityCompat.requestPermissions(
it,
arrayOf(Manifest.permission.CAMERA),
cameraRequest
)
}
imageView = myview.findViewById<View>(R.id.imageView) as ImageView
val photoButton: Button = myview.findViewById<Button>(R.id.button)
photoButton.setOnClickListener {
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, cameraRequest)
}
return myview
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == cameraRequest) {
val photo: Bitmap = data?.extras?.get("data") as Bitmap
imageView.setImageBitmap(photo)
}
if (requestCode == IMAGE_REQUEST_CODE && resultCode == AppCompatActivity.RESULT_OK) {
imageView.setImageURI(data?.data)
}
}
private fun pickImageGallery() {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, IMAGE_REQUEST_CODE)
}
}