Samuel Wittlinger
Samuel Wittlinger

Reputation: 15

Kotlin - Share audio file from raw resource

I have a soundboard app for android and am trying to make users be able to share the sounds in the app via messenger, gmail etc. This is the code I've tried to use for that purpose:

Fragment:

val uri = SoundProvider.getUri(4,(activity as MainActivity).packageName)
val share = Intent(Intent.ACTION_SEND)
share.type = "audio/*"
share.putExtra(Intent.EXTRA_STREAM, uri)
startActivity(Intent.createChooser(share, "Share Sound File"))

getUri function:

fun getUri(id: Int, packageName: String):Uri{
        val uri = Uri.parse(
            ContentResolver.SCHEME_ANDROID_RESOURCE
                    + File.pathSeparator + File.separator + File.separator
                    + packageName
                    + File.separator
                    + R.raw.random_sound
        )
        return uri
    }

Unfortunately, this code doesn't seem to work, when I click on one of the share options in the app ( for example gmail), it just opens a blank email with no attachments. Similarly with other apps. Does anyone happen to know how to make this work?

Upvotes: 0

Views: 357

Answers (1)

Saif
Saif

Reputation: 397

I think this is the right way means using File.pathSeparator 2 times and not 3 times consecutively:

ContentResolver.SCHEME_ANDROID_RESOURCE
                + File.pathSeparator + File.separator
                + packageName
                + File.separator
                + R.raw.random_sound

Upvotes: 1

Related Questions