Get path of Android resource

旧时模样 提交于 2019-12-17 03:17:25

问题


My question is: is it possible to get the absolute path of a resource (in a subdirectory of the res/ folder) in Android?

Most of the answers I see to this question on google suggest getting a file descriptor, implying that this is not possible.

Is it?

EDIT: 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.


回答1:


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");

Or use openRawResource(R.id) to open an inputStream, and use it the same way you would use a FileInputStream (readonly)




回答2:


Not possible. The resource folder you see is compiled into the apk, and it may not even store as it is. (e.g. those layout xml files are no longer xml files once it is compiled)

Two possible solution/work-around for your situation:

  1. Push the file you need to a known location (like /mnt/sdcard/... ) with sdk tools
  2. Create a mockup provider for your media loader that accepts stuff in raw resource rather than path, for your testing purpose



回答3:


In general, NO. As it was repeated several times here, you need to access them through the resources.

That said, there are ways you can work around that:

  • copy the files from res/assets to the internal storage on first run.
  • use adb to push the files to any public location, such as the sdcard (note, it is discouraged also to use absolute paths in there, as not every device may mount the drives to the same location)
  • use a rooted phone, and access any folder, e.g., /data/data/yourapp/ or /tmp/

In any case, you would test against a solution that will most probably not work in most of the devices. I suggest you to follow the directions from the dev guide: http://developer.android.com/guide/topics/data/data-storage.html



来源:https://stackoverflow.com/questions/6301493/get-path-of-android-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!