获取Android资源的路径 [英] Get path of Android resource
问题描述
我的问题是:是否可以在Android中获取资源的绝对路径(在res/文件夹的子目录中)?
My question is: is it possible to get the absolute path of a resource (in a subdirectory of the res/ folder) in Android?
我在Google上对该问题的大部分答案都建议获取文件描述符,这暗示不可能.
Most of the answers I see to this question on google suggest getting a file descriptor, implying that this is not possible.
是吗?
之所以要这样做,是因为我正在编写接受媒体路径并播放它的类.如果我可以通过资源的绝对路径,则测试会容易得多.
The reason I want to do this is because I'm writing classes that accept a path to media and play it. Testing would be much easier if I could pass the absolute path of a resource.
推荐答案
Use URI Paths instead of "absolute" path, see this post
Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon");
或者使用openRawResource(R.id)打开inputStream,并使用与使用FileInputStream(只读)相同的方式
Or use openRawResource(R.id) to open an inputStream, and use it the same way you would use a FileInputStream (readonly)
这篇关于获取Android资源的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!