Android (Kotlin): FileProvider to share mp3 file

436 Views Asked by At

I am coding an app that shows some mp3 files in a card view and I am trying to set a "Share" button to send the files to other apps, one by one. I am using FileProvider. I managed to share a text, but I can't do it with my files. I am accessing all the mp3 files in my external storage with a constructor, they are not mp3 stored in a "raw" folder in Android Studio.

inner class MySongAdapter : BaseAdapter {
    private val authorities = "com.example.loadmedia_sdcard_demo.fileprovider"
    private lateinit var mainAudio: File
    var myListSong = ArrayList<SongInfo>()
    constructor(myListSong: ArrayList<SongInfo>) : super() {
        this.myListSong = myListSong
    }

    override fun getView(position: Int, p1: View?, p2: ViewGroup?): View {
        var myview = layoutInflater.inflate(R.layout.mylayout, null)
        var song : SongInfo = myListSong[position]
        myview.textView1.text = song.Title
        myview.textView2.text = song.Author

        myview.share_button.setOnClickListener {
            val path = FileProvider.getUriForFile(this@MainActivity, authorities, mainAudio)

            val shareIntent = Intent()
            shareIntent.action = Intent.ACTION_SEND
            shareIntent.putExtra(Intent.EXTRA_STREAM, path)
            shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            shareIntent.type = "audio/*"
            startActivity(Intent.createChooser(shareIntent, "Share..."))
        }

The problem is inside val path, it is requesting a file.

val path = FileProvider.getUriForFile(this@MainActivity, authorities, mainAudio) // here is the problem

Any idea of how to do it? Thank you very much.

Edit:

Stack Trace:

D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.loadmedia_sdcard_demo, PID: 21603 kotlin.UninitializedPropertyAccessException: lateinit property mainAudio has not been initialized at com.example.loadmedia_sdcard_demo.MainActivity$MySongAdapter.getView$lambda-0(MainActivity.kt:61) at com.example.loadmedia_sdcard_demo.MainActivity$MySongAdapter.$r8$lambda$hlRnJvjsvoebk3BD6eX7OpstO8M(Unknown Source:0) at com.example.loadmedia_sdcard_demo.MainActivity$MySongAdapter$$ExternalSyntheticLambda0.onClick(Unknown Source:4) at android.view.View.performClick(View.java:7125) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194) at android.view.View.performClickInternal(View.java:7102) at android.view.View.access$3500(View.java:801) at android.view.View$PerformClick.run(View.java:27336) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) I/Process: Sending signal. PID: 21603 SIG: 9

Problem in detail: I am trying to share an mp3 file with, for instance, Telegram. When I click the "Share" button the app crashes.

1

There are 1 best solutions below

1
On
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.loadmedia_sdcard_demo, PID: 21603 kotlin.UninitializedPropertyAccessException: lateinit property mainAudio has not been initialized at com.example.loadmedia_sdcard_demo.MainActivity$MySongAdapter.getView$lambda-0(MainActivity.kt:61)

You declared mainAudio as:

private lateinit var mainAudio: File

However, you never assigned a value to it, before you tried using it here:

val path = FileProvider.getUriForFile(this@MainActivity, authorities, mainAudio)

You need to assign a value to a lateinit var before you try using it, otherwise you will get this exception.