Luis Fernandez
Luis Fernandez

Reputation: 559

Get the Path of a .mp3 raw resource from URI

I'm trying to send an audio file to Dialogflow from an Android app.

In order to do that I need the Path of the audio file. According to the documentation, it goes like this:

val videouri: URI = URI.create("android.resource://$packageName/raw/tts")
val videopath: Path = Paths.get(videouri)
val inputAudio: ByteArray = Files.readAllBytes(videopath)

However, on the second line, when obtaining the Path I get the following exception:

java.nio.file.filesystemnotfoundexception provider android.resource not installed

The audio file is located in res > raw > tts.mp3

Upvotes: 1

Views: 552

Answers (2)

majster
majster

Reputation: 120

Use Resources.openRawResource() with the resource ID, which is R.raw.filename. See https://developer.android.com/guide/topics/resources/providing-resources

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006614

First, raw resources are not files on the filesystem of the device.

Second, a URI is not a file.

Use Resources and openRawResource() to get an InputStream on the resource. IIRC, there is a readBytes() extension function in Kotlin that you can use to read in the contents.

Upvotes: 2

Related Questions