I'm creating a kotlin app that reads an audio file from external storage from a device. However when I try to read it I get fileNotFoundException. Here is my code:
val file = File(newAudio!!)
val inputStream = context?.contentResolver?.openInputStream(Uri.fromFile(file))
val requestFile = RequestBody.create(MediaType.parse("audio/mpeg"), inputStream?.readBytes())
newAudio
equals intent's path (URI). Why is this happening?
EDIT: Here is how I get newAudio and send it to ViewModel:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 111 && resultCode == Activity.RESULT_OK && data != null) {
fileData = data.data!!
newAudioViewModel!!.sendNewAudio(fileData)
navHostFragment.findNavController().navigate(R.id.newAudioInfoFragment)
}
}
Then I get it from ViewModel like this:
newAudioViewModel!!.getNewAudio().observe(viewLifecycleOwner, Observer<Uri?> { audio ->
newAudio = audio?.path
})