在Android Studio中遇到在资源文件夹中定位视频文件的问题。

huangapple go评论93阅读模式
英文:

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()

huangapple
  • 本文由 发表于 2023年6月6日 14:51:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定