英文:
Facing problems in Android Studio in locating video file in the resource folder
问题
我正在创建一个视频播放器作为我的创业项目的一部分。我已经创建了一个名为raw的Android资源目录文件。当我在主活动中定位该文件时,它显示了以下Kotlin代码的错误:
val uri: Uri = Uri.parse("android.resource://" + packageName + "/test")
有人可以帮助我解决上述代码的问题吗?因为我遇到了一个错误。
英文:
I am creating a video player as part of my startup project. I have made an Android Resource Directory file that I named as raw. When I locate the file in the main activity it shows an error to the following Kotlin code:
val uri: Uri = parse(uriString: "android.resource://" + packageName + "/" + "test" )
Can someone please help me with the above code because I'm getting an error?
答案1
得分: 2
你需要使用文件的资源标识而不是文件名:
val uri: Uri = Uri.parse("android.resource://" + packageName + "/" + R.raw.test)
为了使代码更清晰,您可以使用以下方式:
val uri = Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(packageName)
.appendPath("${R.raw.test}")
.build()
英文:
You need to use the resource id of the file not the file name:
val uri: Uri = Uri.parse(uriString: "android.resource://" + packageName + "/" + R.raw.test)
To have cleaner code you can use this:
val uri = Uri.Builder()
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.authority(packageName)
.appendPath("${R.raw.test}")
.build()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论