in

I can’t upload images into Firebase Storage in Kotlin


imageUpload.setOnClickListener {
        if (ContextCompat.checkSelfPermission(requireContext(),android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            if (ActivityCompat.shouldShowRequestPermissionRationale(requireActivity(),android.Manifest.permission.READ_EXTERNAL_STORAGE)){
                Snackbar.make(view,"Galeriye erişmek için izin vermeniz gerekmektedir.",Snackbar.LENGTH_INDEFINITE).setAction("İzin Ver"){
                    permissionLauncher.launch(android.Manifest.permission.READ_EXTERNAL_STORAGE)
                }.show()
            }else{
                permissionLauncher.launch(android.Manifest.permission.READ_EXTERNAL_STORAGE)
            }
        }else{
            val intentToGallery = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
            activityResultLauncher.launch(intentToGallery)
        }

        val uuid = UUID.randomUUID()
        val imageName = "$uuid.jpg"

        val reference = storage.reference
        val imageReference = reference.child("images").child(imageName)

        if (selectedPicture!=null){
            imageReference.putFile(selectedPicture!!).addOnSuccessListener {
                val uploadPictureReference = storage.reference.child("images").child(imageName)
                uploadPictureReference.downloadUrl.addOnSuccessListener {
                    val downloadUrl = it.toString()

                    val profileMap = hashMapOf<String, Any>()
                    profileMap.put("downloadUrl",downloadUrl)

                    firestore.collection("Profile").add(profileMap).addOnSuccessListener {
                        
                    }.addOnFailureListener {
                        Toast.makeText(requireContext(),it.localizedMessage,Toast.LENGTH_SHORT).show()
                    }
                }

            }.addOnFailureListener {
                Toast.makeText(requireContext(),it.localizedMessage,Toast.LENGTH_SHORT).show()
            }
        }
    }
    forgotPassword.setOnClickListener {
        val action = ProfileFragmentDirections.actionProfileFragmentToForgotPassword2()
        Navigation.findNavController(it).navigate(action)
    }
}



private fun registerLauncher(){
    activityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){result ->
        if (result.resultCode == RESULT_OK){
            val intentFromResult = result.data
            if (intentFromResult !=null){
                selectedPicture = intentFromResult.data
                selectedPicture?.let {
                    binding.selectImage.setImageURI(it)
                }
            }
        }
    }
    permissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()){result->
        if (result){
            val intentToGallery = Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
            activityResultLauncher.launch(intentToGallery)
        }else{
            Toast.makeText(requireContext(),"Galeriye Erişim İçin İzin Gereklidir.", Toast.LENGTH_SHORT).show()
        }
    }

I am able to show the permissions while uploading the image. I can see Toast, snackbar messages depending on whether I give permission or not. I am able to upload the photo at first. However, since this photo I uploaded is not saved in the storage, when I change the page, the profile photo becomes old. So it doesn’t add the new photo I saved. I’m trying to upload a photo in a Fragment.



Source: https://stackoverflow.com/questions/70543946/i-cant-upload-images-into-firebase-storage-in-kotlin

BDD-driven natural language automated testing framework, present by Trip Flight

Minecraft Plugin ClassNotFound Error from External JAR