BlueVoodoo
BlueVoodoo

Reputation: 3686

Accessing a resource file in Android

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.

Upvotes: 2

Views: 1393

Answers (2)

Barak
Barak

Reputation: 16393

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.

Upvotes: 3

MAC
MAC

Reputation: 15847

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

Upvotes: 0

Related Questions