jason white
jason white

Reputation: 711

Android resource files

I have a cheerapp.mp3 in my /res/raw folder

so my code is

String filepath="/res/raw/cheerapp";   //or cheerapp.mp3
file = new File(filePath); 
FileInputStream in = null;
try {
    in = new FileInputStream( file );  
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

The error I got file not found. why?

Upvotes: 0

Views: 167

Answers (3)

Kelvin Trinh
Kelvin Trinh

Reputation: 1248

Never can you access resource files by path!

Because they are compiled in APK file installed in Android. You can only access resource within application by access its generated resource id, from any activity (context):

InputStream cheerSound = this.getResources().openRawResource(R.raw.cheerapp);

or from view:

InputStream cheerSound = this.getContext().getResources().openRawResource(R.raw.cheerapp);

In your case, you should store sound files in external sd-card, then can access them by path. For e.g, you store your file in sounds folder on your sd-card:

FileInputStream inFile = new FileInputStream("/mnt/sdcard/sounds/cheerapp.mp3");

NOTE: path starts with '/' is absolute path, because '/' represents root in Unix-like OS (Unix, Linux, Android, ...)

Upvotes: 1

Kohakukun
Kohakukun

Reputation: 276

Maybe you could use the AssetManager to manage your resources. For example:

AssetManager manager = this.getContext().getAssets();
InputStream open;
try {
    open = manager.open(fileName);
    BitmapFactory.Options options = new BitmapFactory.Options();
    ...
} catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 0

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21201

Use assets folder and ...

InputStream sound = getAssets().open("filename.mp3");

... or raw folder and ...

InputStream sound = getResources().openRawResource(R.raw.filename);

if it doesn't help you, then check this

Upvotes: 3

Related Questions