0

Hi

I've been implementing function that allows user to launch e-mail app within my own.

fun composeEmail(address: Array<String>,message : String, subject: String, attachment : Uri){
    val intent = Intent(Intent.ACTION_SENDTO).apply{
        data = Uri.parse("mailto:")
        putExtra(Intent.EXTRA_EMAIL,address)
        putExtra(Intent.EXTRA_SUBJECT,subject)
        putExtra(Intent.EXTRA_TEXT,message)
        putExtra(Intent.EXTRA_STREAM,attachment)
    }
    if(intent.resolveActivity(requireActivity().packageManager) != null){
        startActivity(intent)
    }
}

I want to make possible attaching files, and I'm using another intent for this:


    private var uriAt : Uri = Uri.EMPTY     
fun selectFile()
    {
        val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
            type = "*/*"
        }
        if (intent.resolveActivity(requireActivity().packageManager) != null)
            startActivityForResult(intent,8)

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(requestCode == 8 && resultCode == Activity.RESULT_OK){

            if (data != null) {
                uriAt = data.data!!
            }

        }
    }

There's code responsible for triggering Intents

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val emailText :EditText = binding.emailText
    val emailMessage  :EditText = binding.emailMessage
    val emailSubject : EditText = binding.emailSubject

    binding.sendButton.setOnClickListener {
                composeEmail(address = arrayOf(emailText.text.toString()),message =  emailMessage.text.toString(),subject = emailSubject.text.toString(),attachment = uriAt)
    }
    binding.attachmentButton.setOnClickListener {
        selectFile()
    }
}

App is starting successfully but when attachment has been selected and activity's starting email Intent, e-mail app that's been launched sends message that attachment cannot be attach.

I've read another post, and implemented certain permissions in manifest eg.

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
amtrax
  • 466
  • 7
  • 20

0 Answers0