1

After searching for solution, I have found from this link that it is possible to retrieve path like:

Using Resource Id

Syntax : android.resource://[package]/[resource_id]

Example : Uri.parse("android.resource://com.my.package/" + R.raw.mp3filename);

Which is exactly what I want. Problem is that, I'm using lolipop, and it does not work. When the package/resource is parsed, path from Uri.parse will be null. Is there other way around to write this, since in my Song class, I have only access to resourceId (R.raw.mp3filename).

Community
  • 1
  • 1
Kapparino
  • 988
  • 11
  • 33
  • "and it seems it does not work" -- what is "it"? "returns null for path" -- *what* returns `null`? `Uri.parse()`? – CommonsWare Apr 23 '16 at 22:05
  • @CommonsWare It returns null for path when I try to parse the above example – Kapparino Apr 23 '16 at 22:19
  • What do you mean by "null for path"? A `Uri` is not a path, any more than `http://stackoverflow.com/questions/36816944/android-how-to-get-path-of-mp3-stored-in-raw-folder?noredirect=1#comment61206631_36816944` is a path. Are you saying that `Uri.parse()` itself is returning `null`? Or are you trying to do something with that `Uri` (e.g., call `getPath()` on it), and *that* is returning `null`? – CommonsWare Apr 23 '16 at 22:43
  • Alright, I have a simple question now: "Is it possible to get file path by using R.raw.mp3file" and how? - And to answer on your question, on what i'm going to do with URI, yes i was trying with Uri instance to call getPath() method, which in my case is returning null. – Kapparino Apr 23 '16 at 22:47

1 Answers1

3

Is it possible to get file path by using R.raw.mp3file

No, because it is not a file on the device. It is a file on the hard drive of your development machine. On the device, it is merely an entry in an APK file.

Either:

  • Do whatever you are trying to do some other way that does not involve a file path, or

  • Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of that resource to a local file, such as on internal storage

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491