0

cheerapp.mp3我的/res/raw文件夹中有一个

所以我的代码是

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

我找不到文件的错误。为什么?

4

3 回答 3

3

使用assets文件夹和...

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

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

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

如果它对您没有帮助,请检查

于 2013-02-26T03:57:20.240 回答
1

你永远不能通过路径访问资源文件!

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

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

或从视图:

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

在您的情况下,您应该将声音文件存储在外部 sd 卡中,然后可以通过路径访问它们。例如,您将文件存储在soundssd 卡上的文件夹中:

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

注意:以'/'开头的路径是绝对路径,因为'/'代表类Unix操作系统(Unix,Linux,Android,...)中的root

于 2013-02-26T04:26:51.327 回答
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();
}
于 2013-02-26T03:58:42.913 回答