Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get path of Android resource

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.

like image 450
Prime Avatar asked Jun 10 '11 02:06

Prime


People also ask

What is resource folder in Android?

The res/values folder is used to store the values for the resources that are used in many Android projects to include features of color, styles, dimensions etc.

What is the name of resource file in Android?

The resources locates inside res directory of your app. The Android resource compiler processes resources arcording to which subforder they are in and the format of the file, for example: res/drawable : Contains all resources file that can be drawable, such as (. png, .


2 Answers

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)

like image 141
HPP Avatar answered Sep 23 '22 23:09

HPP


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. Store the files in the '/assets' directory
  2. Push the file you need to a known location (like /mnt/sdcard/... ) with sdk tools
  3. Create a mockup provider for your media loader that accepts stuff in raw resource rather than path, for your testing purpose
like image 33
xandy Avatar answered Sep 23 '22 23:09

xandy