2013-02-26 96 views
0

我有一个cheerapp.mp3在我/res/raw文件夹的Android资源文件

所以我的代码是

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(); 
} 

我得到了文件未找到错误。为什么?

+1

在此处发布您的错误 – 2013-02-26 03:52:18

回答

1

决不可你按路径访问的资源文件!

因为它们是在Android中安装的APK文件中编译的。您只能应用内访问接入资源及其生成的资源ID,从任何活动(上下文):

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

或视图:

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

在你的情况,你应该存储在外部声音文件SD卡,然后可以通过路径访问它们。对于例如,你在sounds文件夹中的文件存储到SD卡上:

​​

注:路径以“/”是绝对路径,因为“/”代表的类Unix操作系统根(Unix的开始,Linux,Android ...)

3

使用assets文件夹和...

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

...或raw文件夹和...

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

,如果它不能帮助你,然后检查this

0

也许你可以使用AssetManager来管理你的资源。例如:

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