2

I've been fiddling around with this for quite some time. I can't work out why my file /res/raw/video.mp4 can't be found by my Android app. I am on API 15 and the phone is up to date with v4.0.3.

The test code currently looks like this:

String file = "android.resource://" + getPackageName() + "/" + R.raw.movie; 
File f = new File(file);
message = f.exists() ? "file exists" : "file missing";

All combinations I have tried so far results in the message string being returned as "file missing". What is the correct way to access a file from the resources? Tried the obvious ones "/res/raw/video.mp4" etc too but all failed.

BlueVoodoo
  • 3,626
  • 5
  • 29
  • 37
  • This answer might come helpful? http://stackoverflow.com/a/2856501/1063730 – nuala Jun 06 '12 at 14:29
  • Yes, it seems I am able to open it up in an inputstream using that one but I need to get the address as a string in order to use ThumbnailUtils.createVideoThumbnail() which is my main goal (but i'd like to get to the bottom of why my example is not working regardless). – BlueVoodoo Jun 06 '12 at 14:34
  • Well, that'll teach me to read the comments before posting an answer... yoshi posted a link saying the same thing as my answer. :p – Barak Jun 06 '12 at 14:41
  • It's not working becasue there is no directoy structure in a compiled apk. It's just data. The framework knows where/how to look for different things in there (`getResources()`, etc) , but it's not a directory structure as we think of it at all. – Barak Jun 06 '12 at 14:49

2 Answers2

3

Assuming you want to open a stream to process the file you should be doing this:

InputStream raw = getResources().openRawResource(R.raw.movie); 

EDIT

Ok, now that I know exactly what you want, I have bad news. Not really possible.

See this SO question

Basically you would need to copy the file out of resources to somewhere that you can get at it like the SD card or internal storage.

Community
  • 1
  • 1
Barak
  • 16,318
  • 9
  • 52
  • 84
0

String url = "file:///android_asset/raw/filename.jpg";

MAC
  • 15,799
  • 8
  • 54
  • 95
  • Same result. Tried "new File("file:///android_asset/raw/video.mp4")". Also tried "file:///android_asset/res/raw/video.mp4" but that too didn't work. – BlueVoodoo Jun 06 '12 at 14:40